blob: 91954ac54b8ab2ab2ed37c0dfe5d8aae46c5d245 [file] [log] [blame]
Marian Balakowicz41d71ed2008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
Marian Balakowicz9c701e82008-01-31 13:57:17 +010025
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +010027#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
Marian Balakowicz902ce702008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicza1cc1472008-02-21 17:27:41 +010042#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
Marian Balakowicze227fbb2008-02-29 21:24:06 +010046#include <image.h>
47
Stephen Warren6904b6e2011-10-18 11:11:49 +000048#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010049#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010052#endif
53
54#if defined(CONFIG_FIT)
Andy Fleming72c23be2008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010056#include <sha1.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010057
Stephen Warren6904b6e2011-10-18 11:11:49 +000058static int fit_check_ramdisk(const void *fit, int os_noffset,
Marian Balakowicza50d5152008-03-12 10:12:37 +010059 uint8_t arch, int verify);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010060#endif
61
Marian Balakowicz2efc2642008-01-31 13:58:13 +010062#ifdef CONFIG_CMD_BDI
Wolfgang Denk6262d0212010-06-28 22:00:46 +020063extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowicz2efc2642008-01-31 13:58:13 +010064#endif
65
66DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz61fde552008-02-27 11:01:04 +010067
Stephen Warren6904b6e2011-10-18 11:11:49 +000068static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +010069 int verify);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010070#else
Marian Balakowicz95418502008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming72c23be2008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010076
Mike Frysingercf2feb12010-10-20 07:17:39 -040077static const table_entry_t uimage_arch[] = {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010078 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010087 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Erickson80f51b82008-05-04 16:45:01 -070088 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010089 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin354b4e32011-10-19 20:41:09 +000096 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson15c669c2011-11-26 19:04:50 +000097 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010098 { -1, "", "", },
99};
100
Mike Frysingercf2feb12010-10-20 07:17:39 -0400101static const table_entry_t uimage_os[] = {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100102 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100103 { IH_OS_LINUX, "linux", "Linux", },
104#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
105 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
106#endif
107 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren29a58662010-09-28 11:05:36 +0200108 { IH_OS_OSE, "ose", "Enea OSE", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100109 { IH_OS_RTEMS, "rtems", "RTEMS", },
110 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
111#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
112 { IH_OS_QNX, "qnx", "QNX", },
113 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
114#endif
Peter Tyser56b8dd12008-09-08 14:56:49 -0500115#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
116 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
117#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100118#ifdef USE_HOSTCC
119 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
120 { IH_OS_DELL, "dell", "Dell", },
121 { IH_OS_ESIX, "esix", "Esix", },
122 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
123 { IH_OS_IRIX, "irix", "Irix", },
124 { IH_OS_NCR, "ncr", "NCR", },
125 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
126 { IH_OS_PSOS, "psos", "pSOS", },
127 { IH_OS_SCO, "sco", "SCO", },
128 { IH_OS_SOLARIS, "solaris", "Solaris", },
129 { IH_OS_SVR4, "svr4", "SVR4", },
130#endif
131 { -1, "", "", },
132};
133
Mike Frysingercf2feb12010-10-20 07:17:39 -0400134static const table_entry_t uimage_type[] = {
Stefano Babic38855512011-10-17 00:07:43 +0000135 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100136 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
137 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby34d12a12011-07-21 09:10:30 -0400138 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100139 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warren8c87eba2011-11-10 13:17:53 -0700140 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic38855512011-10-17 00:07:43 +0000141 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
142 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
143 { IH_TYPE_INVALID, NULL, "Invalid Image", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic38855512011-10-17 00:07:43 +0000145 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100146 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
147 { IH_TYPE_SCRIPT, "script", "Script", },
148 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher58cb84d2011-07-16 00:06:42 +0000149 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100150 { -1, "", "", },
151};
152
Mike Frysingercf2feb12010-10-20 07:17:39 -0400153static const table_entry_t uimage_comp[] = {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100154 { IH_COMP_NONE, "none", "uncompressed", },
155 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
156 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +0200157 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100158 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100159 { -1, "", "", },
160};
161
Stephen Warren6904b6e2011-10-18 11:11:49 +0000162uint32_t crc32(uint32_t, const unsigned char *, uint);
163uint32_t crc32_wd(uint32_t, const unsigned char *, uint, uint);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100164#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000165static void genimg_print_time(time_t timestamp);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100166#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100167
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100168/*****************************************************************************/
169/* Legacy format routines */
170/*****************************************************************************/
Stephen Warren6904b6e2011-10-18 11:11:49 +0000171int image_check_hcrc(const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100172{
173 ulong hcrc;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000174 ulong len = image_get_header_size();
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100175 image_header_t header;
176
177 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000178 memmove(&header, (char *)hdr, image_get_header_size());
179 image_set_hcrc(&header, 0);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100180
Stephen Warren6904b6e2011-10-18 11:11:49 +0000181 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100182
Stephen Warren6904b6e2011-10-18 11:11:49 +0000183 return (hcrc == image_get_hcrc(hdr));
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100184}
185
Stephen Warren6904b6e2011-10-18 11:11:49 +0000186int image_check_dcrc(const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100187{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000188 ulong data = image_get_data(hdr);
189 ulong len = image_get_data_size(hdr);
190 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100191
Stephen Warren6904b6e2011-10-18 11:11:49 +0000192 return (dcrc == image_get_dcrc(hdr));
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100193}
194
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100195/**
196 * image_multi_count - get component (sub-image) count
197 * @hdr: pointer to the header of the multi component image
198 *
199 * image_multi_count() returns number of components in a multi
200 * component image.
201 *
202 * Note: no checking of the image type is done, caller must pass
203 * a valid multi component image.
204 *
205 * returns:
206 * number of components
207 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000208ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100209{
210 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100211 uint32_t *size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100212
213 /* get start of the image payload, which in case of multi
214 * component images that points to a table of component sizes */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000215 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100216
217 /* count non empty slots */
218 for (i = 0; size[i]; ++i)
219 count++;
220
221 return count;
222}
223
224/**
225 * image_multi_getimg - get component data address and size
226 * @hdr: pointer to the header of the multi component image
227 * @idx: index of the requested component
228 * @data: pointer to a ulong variable, will hold component data address
229 * @len: pointer to a ulong variable, will hold component size
230 *
231 * image_multi_getimg() returns size and data address for the requested
232 * component in a multi component image.
233 *
234 * Note: no checking of the image type is done, caller must pass
235 * a valid multi component image.
236 *
237 * returns:
238 * data address and size of the component, if idx is valid
239 * 0 in data and len, if idx is out of range
240 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000241void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100242 ulong *data, ulong *len)
243{
244 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100245 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700246 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100247
248 /* get number of component */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000249 count = image_multi_count(hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100250
251 /* get start of the image payload, which in case of multi
252 * component images that points to a table of component sizes */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000253 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100254
255 /* get address of the proper component data start, which means
256 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000257 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100258
259 if (idx < count) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000260 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100261 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100262
263 /* go over all indices preceding requested component idx */
264 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-05-10 14:02:04 -0700265 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000266 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100267 }
268
269 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700270 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100271 } else {
272 *len = 0;
273 *data = 0;
274 }
275}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100276
Stephen Warren6904b6e2011-10-18 11:11:49 +0000277static void image_print_type(const image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100278{
279 const char *os, *arch, *type, *comp;
280
Stephen Warren6904b6e2011-10-18 11:11:49 +0000281 os = genimg_get_os_name(image_get_os(hdr));
282 arch = genimg_get_arch_name(image_get_arch(hdr));
283 type = genimg_get_type_name(image_get_type(hdr));
284 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100285
Stephen Warren6904b6e2011-10-18 11:11:49 +0000286 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100287}
288
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100289/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200290 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3b506772009-08-19 11:42:56 +0200291 * @ptr: pointer to the legacy format image header
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100292 * @p: pointer to prefix string
293 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200294 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100295 * The routine prints out all header fields followed by the size/offset data
296 * for MULTI/SCRIPT images.
297 *
298 * returns:
299 * no returned results
300 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000301void image_print_contents(const void *ptr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100302{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200303 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200304 const char *p;
305
306#ifdef USE_HOSTCC
307 p = "";
308#else
309 p = " ";
310#endif
311
Stephen Warren6904b6e2011-10-18 11:11:49 +0000312 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100313#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000314 printf("%sCreated: ", p);
315 genimg_print_time((time_t)image_get_time(hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100316#endif
Stephen Warren6904b6e2011-10-18 11:11:49 +0000317 printf("%sImage Type: ", p);
318 image_print_type(hdr);
319 printf("%sData Size: ", p);
320 genimg_print_size(image_get_data_size(hdr));
321 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
322 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100323
Stephen Warren6904b6e2011-10-18 11:11:49 +0000324 if (image_check_type(hdr, IH_TYPE_MULTI) ||
325 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100326 int i;
327 ulong data, len;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000328 ulong count = image_multi_count(hdr);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100329
Stephen Warren6904b6e2011-10-18 11:11:49 +0000330 printf("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100331 for (i = 0; i < count; i++) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000332 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100333
Stephen Warren6904b6e2011-10-18 11:11:49 +0000334 printf("%s Image %d: ", p, i);
335 genimg_print_size(len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100336
Stephen Warren6904b6e2011-10-18 11:11:49 +0000337 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100338 /*
339 * the user may need to know offsets
340 * if planning to do something with
341 * multiple files
342 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000343 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100344 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100345 }
346 }
347}
348
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100349
350#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100351/**
352 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100353 * @rd_addr: ramdisk image start address
354 * @arch: expected ramdisk architecture
355 * @verify: checksum verification flag
356 *
357 * image_get_ramdisk() returns a pointer to the verified ramdisk image
358 * header. Routine receives image start address and expected architecture
359 * flag. Verification done covers data and header integrity and os/type/arch
360 * fields checking.
361 *
362 * If dataflash support is enabled routine checks for dataflash addresses
363 * and handles required dataflash reads.
364 *
365 * returns:
366 * pointer to a ramdisk image header, if image was found and valid
367 * otherwise, return NULL
368 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000369static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100370 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100371{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200372 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100373
Stephen Warren6904b6e2011-10-18 11:11:49 +0000374 if (!image_check_magic(rd_hdr)) {
375 puts("Bad Magic Number\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000376 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100377 return NULL;
378 }
379
Stephen Warren6904b6e2011-10-18 11:11:49 +0000380 if (!image_check_hcrc(rd_hdr)) {
381 puts("Bad Header Checksum\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000382 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100383 return NULL;
384 }
385
Simon Glass0169e6b2012-02-13 13:51:18 +0000386 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000387 image_print_contents(rd_hdr);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100388
389 if (verify) {
390 puts(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000391 if (!image_check_dcrc(rd_hdr)) {
392 puts("Bad Data CRC\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000393 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100394 return NULL;
395 }
396 puts("OK\n");
397 }
398
Simon Glass0169e6b2012-02-13 13:51:18 +0000399 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100400
Stephen Warren6904b6e2011-10-18 11:11:49 +0000401 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
402 !image_check_arch(rd_hdr, arch) ||
403 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
404 printf("No Linux %s Ramdisk Image\n",
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100405 genimg_get_arch_name(arch));
Simon Glass0169e6b2012-02-13 13:51:18 +0000406 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100407 return NULL;
408 }
409
410 return rd_hdr;
411}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100412#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100413
414/*****************************************************************************/
415/* Shared dual-format routines */
416/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100417#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +0000418int getenv_yesno(char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100419{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000420 char *s = getenv(var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100421 return (s && (*s == 'n')) ? 0 : 1;
422}
423
424ulong getenv_bootm_low(void)
425{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000426 char *s = getenv("bootm_low");
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100427 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000428 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100429 return tmp;
430 }
431
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200432#if defined(CONFIG_SYS_SDRAM_BASE)
433 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100434#elif defined(CONFIG_ARM)
435 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100436#else
437 return 0;
438#endif
439}
440
Becky Bruced26d67c2008-06-09 20:37:18 -0500441phys_size_t getenv_bootm_size(void)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100442{
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500443 phys_size_t tmp;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000444 char *s = getenv("bootm_size");
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100445 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000446 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100447 return tmp;
448 }
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500449 s = getenv("bootm_low");
450 if (s)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000451 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500452 else
453 tmp = 0;
454
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100455
Marian Balakowicz2499b042008-03-12 12:14:15 +0100456#if defined(CONFIG_ARM)
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500457 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100458#else
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500459 return gd->bd->bi_memsize - tmp;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100460#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100461}
462
Grant Likely26396382011-03-28 09:58:43 +0000463phys_size_t getenv_bootm_mapsize(void)
464{
465 phys_size_t tmp;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000466 char *s = getenv("bootm_mapsize");
Grant Likely26396382011-03-28 09:58:43 +0000467 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000468 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likely26396382011-03-28 09:58:43 +0000469 return tmp;
470 }
471
472#if defined(CONFIG_SYS_BOOTMAPSZ)
473 return CONFIG_SYS_BOOTMAPSZ;
474#else
475 return getenv_bootm_size();
476#endif
477}
478
Stephen Warren6904b6e2011-10-18 11:11:49 +0000479void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100480{
Larry Johnsone4551002010-04-20 08:09:43 -0400481 if (to == from)
482 return;
483
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100484#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
485 while (len > 0) {
486 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000487 WATCHDOG_RESET();
488 memmove(to, from, tail);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100489 to += tail;
490 from += tail;
491 len -= tail;
492 }
493#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000494 memmove(to, from, len);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100495#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
496}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100497#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100498
Stephen Warren6904b6e2011-10-18 11:11:49 +0000499void genimg_print_size(uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100500{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100501#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +0000502 printf("%d Bytes = ", size);
503 print_size(size, "\n");
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100504#else
Stephen Warren6904b6e2011-10-18 11:11:49 +0000505 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100506 size, (double)size / 1.024e3,
507 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100508#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100509}
510
511#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000512static void genimg_print_time(time_t timestamp)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100513{
514#ifndef USE_HOSTCC
515 struct rtc_time tm;
516
Stephen Warren6904b6e2011-10-18 11:11:49 +0000517 to_tm(timestamp, &tm);
518 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100519 tm.tm_year, tm.tm_mon, tm.tm_mday,
520 tm.tm_hour, tm.tm_min, tm.tm_sec);
521#else
Stephen Warren6904b6e2011-10-18 11:11:49 +0000522 printf("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100523#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100524}
525#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
526
527/**
528 * get_table_entry_name - translate entry id to long name
529 * @table: pointer to a translation table for entries of a specific type
530 * @msg: message to be returned when translation fails
531 * @id: entry id to be translated
532 *
533 * get_table_entry_name() will go over translation table trying to find
534 * entry that matches given id. If matching entry is found, its long
535 * name is returned to the caller.
536 *
537 * returns:
538 * long entry name if translation succeeds
539 * msg otherwise
540 */
Mike Frysingercf2feb12010-10-20 07:17:39 -0400541char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100542{
543 for (; table->id >= 0; ++table) {
544 if (table->id == id)
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200545#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Wood68498ab2009-04-02 16:15:10 -0500546 return table->lname;
547#else
548 return table->lname + gd->reloc_off;
549#endif
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100550 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100551 return (msg);
552}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100553
Stephen Warren6904b6e2011-10-18 11:11:49 +0000554const char *genimg_get_os_name(uint8_t os)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100555{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000556 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100557}
558
Stephen Warren6904b6e2011-10-18 11:11:49 +0000559const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100560{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000561 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
562 arch));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100563}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100564
Stephen Warren6904b6e2011-10-18 11:11:49 +0000565const char *genimg_get_type_name(uint8_t type)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100566{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000567 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100568}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100569
Stephen Warren6904b6e2011-10-18 11:11:49 +0000570const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100571{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000572 return (get_table_entry_name(uimage_comp, "Unknown Compression",
573 comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100574}
575
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100576/**
577 * get_table_entry_id - translate short entry name to id
578 * @table: pointer to a translation table for entries of a specific type
579 * @table_name: to be used in case of error
580 * @name: entry short name to be translated
581 *
582 * get_table_entry_id() will go over translation table trying to find
583 * entry that matches given short name. If matching entry is found,
584 * its id returned to the caller.
585 *
586 * returns:
587 * entry id if translation succeeds
588 * -1 otherwise
589 */
Mike Frysingercf2feb12010-10-20 07:17:39 -0400590int get_table_entry_id(const table_entry_t *table,
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100591 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100592{
Mike Frysingercf2feb12010-10-20 07:17:39 -0400593 const table_entry_t *t;
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100594#ifdef USE_HOSTCC
595 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100596
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100597 for (t = table; t->id >= 0; ++t) {
598 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000599 return(t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100600 }
601
Stephen Warren6904b6e2011-10-18 11:11:49 +0000602 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100603 for (t = table; t->id >= 0; ++t) {
604 if (t->sname == NULL)
605 continue;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000606 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100607 first = 0;
608 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000609 fprintf(stderr, "\n");
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100610#else
611 for (t = table; t->id >= 0; ++t) {
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200612#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Wood68498ab2009-04-02 16:15:10 -0500613 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200614#else
615 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500616#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100617 return (t->id);
618 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000619 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100620#endif /* USE_HOSTCC */
621 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100622}
623
Stephen Warren6904b6e2011-10-18 11:11:49 +0000624int genimg_get_os_id(const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100625{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000626 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100627}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100628
Stephen Warren6904b6e2011-10-18 11:11:49 +0000629int genimg_get_arch_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100630{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000631 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100632}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100633
Stephen Warren6904b6e2011-10-18 11:11:49 +0000634int genimg_get_type_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100635{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000636 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100637}
Marian Balakowicz95418502008-01-31 13:55:39 +0100638
Stephen Warren6904b6e2011-10-18 11:11:49 +0000639int genimg_get_comp_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100640{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000641 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100642}
643
644#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100645/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100646 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100647 * @img_addr: image start address
648 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100649 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100650 * legacy or FIT image.
651 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100652 * New uImage format and FDT blob are based on a libfdt. FDT blob
653 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100654 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100655 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100656 * returns:
657 * image format type or IMAGE_FORMAT_INVALID if no image is present
658 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000659int genimg_get_format(void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100660{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200661 ulong format = IMAGE_FORMAT_INVALID;
662 const image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100663#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3b506772009-08-19 11:42:56 +0200664 char *fit_hdr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100665#endif
666
Wolfgang Denk3b506772009-08-19 11:42:56 +0200667 hdr = (const image_header_t *)img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100668 if (image_check_magic(hdr))
669 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100670#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100671 else {
672 fit_hdr = (char *)img_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000673 if (fdt_check_header(fit_hdr) == 0)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100674 format = IMAGE_FORMAT_FIT;
675 }
676#endif
677
678 return format;
679}
680
681/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100682 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100683 * @img_addr: image start address
684 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100685 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100686 * in a dataflash storage. If so, image is moved to a system RAM memory.
687 *
688 * returns:
689 * image start address after possible relocation from special storage
690 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000691ulong genimg_get_image(ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100692{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100693 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100694
695#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100696 ulong h_size, d_size;
697
Stephen Warren6904b6e2011-10-18 11:11:49 +0000698 if (addr_dataflash(img_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100699 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200700 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100701
702 /* get header size */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000703 h_size = image_get_header_size();
Marian Balakowicz535b9102008-02-27 11:00:47 +0100704#if defined(CONFIG_FIT)
705 if (sizeof(struct fdt_header) > h_size)
706 h_size = sizeof(struct fdt_header);
707#endif
708
709 /* read in header */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000710 debug(" Reading image header from dataflash address "
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100711 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100712
Stephen Warren6904b6e2011-10-18 11:11:49 +0000713 read_dataflash(img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100714
Marian Balakowicz535b9102008-02-27 11:00:47 +0100715 /* get data size */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000716 switch (genimg_get_format((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100717 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000718 d_size = image_get_data_size(
719 (const image_header_t *)ram_addr);
720 debug(" Legacy format image found at 0x%08lx, "
721 "size 0x%08lx\n",
Marian Balakowicz535b9102008-02-27 11:00:47 +0100722 ram_addr, d_size);
723 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100724#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100725 case IMAGE_FORMAT_FIT:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000726 d_size = fit_get_size((const void *)ram_addr) - h_size;
727 debug(" FIT/FDT format image found at 0x%08lx, "
728 "size 0x%08lx\n",
Marian Balakowicz535b9102008-02-27 11:00:47 +0100729 ram_addr, d_size);
730 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100731#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100732 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000733 printf(" No valid image found at 0x%08lx\n",
734 img_addr);
Marian Balakowicz535b9102008-02-27 11:00:47 +0100735 return ram_addr;
736 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100737
Marian Balakowicz535b9102008-02-27 11:00:47 +0100738 /* read in image data */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000739 debug(" Reading image remaining data from dataflash address "
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100740 "%08lx to RAM address %08lx\n", img_addr + h_size,
741 ram_addr + h_size);
742
Stephen Warren6904b6e2011-10-18 11:11:49 +0000743 read_dataflash(img_addr + h_size, d_size,
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100744 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100745
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100746 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100747#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100748
749 return ram_addr;
750}
751
752/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100753 * fit_has_config - check if there is a valid FIT configuration
754 * @images: pointer to the bootm command headers structure
755 *
756 * fit_has_config() checks if there is a FIT configuration in use
757 * (if FTI support is present).
758 *
759 * returns:
760 * 0, no FIT support or no configuration found
761 * 1, configuration found
762 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000763int genimg_has_config(bootm_headers_t *images)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100764{
765#if defined(CONFIG_FIT)
766 if (images->fit_uname_cfg)
767 return 1;
768#endif
769 return 0;
770}
771
772/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100773 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100774 * @argc: command argument count
775 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100776 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100777 * @arch: expected ramdisk architecture
778 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
779 * @rd_end: pointer to a ulong variable, will hold ramdisk end
780 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100781 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100782 * Curently supported are the following ramdisk sources:
783 * - multicomponent kernel/ramdisk image,
784 * - commandline provided address of decicated ramdisk image.
785 *
786 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100787 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100788 * rd_start and rd_end are set to ramdisk start/end addresses if
789 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100790 *
Kumar Galab8ca7342008-08-29 19:08:29 -0500791 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz95418502008-01-31 13:55:39 +0100792 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100793 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000794int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100795 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100796{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100797 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100798 ulong rd_data, rd_len;
Wolfgang Denk3b506772009-08-19 11:42:56 +0200799 const image_header_t *rd_hdr;
Marek Vasut6988d132012-03-30 23:19:10 +0200800#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut28850d02012-03-18 11:47:58 +0000801 char *end;
Marek Vasut6988d132012-03-30 23:19:10 +0200802#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100803#if defined(CONFIG_FIT)
804 void *fit_hdr;
805 const char *fit_uname_config = NULL;
806 const char *fit_uname_ramdisk = NULL;
807 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100808 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100809 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100810 const void *data;
811 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100812#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100813
Marian Balakowicza50d5152008-03-12 10:12:37 +0100814 *rd_start = 0;
815 *rd_end = 0;
816
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100817 /*
818 * Look for a '-' which indicates to ignore the
819 * ramdisk argument
820 */
821 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000822 debug("## Skipping init Ramdisk\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100823 rd_len = rd_data = 0;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000824 } else if (argc >= 3 || genimg_has_config(images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100825#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100826 if (argc >= 3) {
827 /*
828 * If the init ramdisk comes from the FIT image and
829 * the FIT image address is omitted in the command
830 * line argument, try to use os FIT image address or
831 * default load address.
832 */
833 if (images->fit_uname_os)
834 default_addr = (ulong)images->fit_hdr_os;
835 else
836 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100837
Stephen Warren6904b6e2011-10-18 11:11:49 +0000838 if (fit_parse_conf(argv[2], default_addr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100839 &rd_addr, &fit_uname_config)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000840 debug("* ramdisk: config '%s' from image at "
841 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100842 fit_uname_config, rd_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000843 } else if (fit_parse_subimage(argv[2], default_addr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100844 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000845 debug("* ramdisk: subimage '%s' from image at "
846 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100847 fit_uname_ramdisk, rd_addr);
848 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100849#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100850 {
851 rd_addr = simple_strtoul(argv[2], NULL, 16);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000852 debug("* ramdisk: cmdline image address = "
853 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100854 rd_addr);
855 }
856#if defined(CONFIG_FIT)
857 } else {
858 /* use FIT configuration provided in first bootm
859 * command argument
860 */
861 rd_addr = (ulong)images->fit_hdr_os;
862 fit_uname_config = images->fit_uname_cfg;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000863 debug("* ramdisk: using config '%s' from image "
864 "at 0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100865 fit_uname_config, rd_addr);
866
867 /*
868 * Check whether configuration has ramdisk defined,
869 * if not, don't try to use it, quit silently.
870 */
871 fit_hdr = (void *)rd_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000872 cfg_noffset = fit_conf_get_node(fit_hdr,
873 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100874 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000875 debug("* ramdisk: no such config\n");
Michal Simekb2474002008-07-11 10:43:13 +0200876 return 1;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100877 }
878
Stephen Warren6904b6e2011-10-18 11:11:49 +0000879 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
880 cfg_noffset);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100881 if (rd_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000882 debug("* ramdisk: no ramdisk in config\n");
Peter Tyser190ee482008-08-05 10:51:57 -0500883 return 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100884 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100885 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100886#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100887
888 /* copy from dataflash if needed */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000889 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100890
891 /*
892 * Check if there is an initrd image at the
893 * address provided in the second bootm argument
894 * check image type, for FIT images get FIT node.
895 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000896 switch (genimg_get_format((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100897 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000898 printf("## Loading init Ramdisk from Legacy "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100899 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100900
Simon Glass0169e6b2012-02-13 13:51:18 +0000901 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000902 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100903 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100904
Marian Balakowicza50d5152008-03-12 10:12:37 +0100905 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600906 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600907
Stephen Warren6904b6e2011-10-18 11:11:49 +0000908 rd_data = image_get_data(rd_hdr);
909 rd_len = image_get_data_size(rd_hdr);
910 rd_load = image_get_load(rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100911 break;
912#if defined(CONFIG_FIT)
913 case IMAGE_FORMAT_FIT:
914 fit_hdr = (void *)rd_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000915 printf("## Loading init Ramdisk from FIT "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100916 "Image at %08lx ...\n", rd_addr);
917
Simon Glass0169e6b2012-02-13 13:51:18 +0000918 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000919 if (!fit_check_format(fit_hdr)) {
920 puts("Bad FIT ramdisk image format!\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000921 bootstage_error(
Simon Glass52823fd2011-12-10 11:08:02 +0000922 BOOTSTAGE_ID_FIT_RD_FORMAT);
Michal Simekb2474002008-07-11 10:43:13 +0200923 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100924 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000925 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100926
927 if (!fit_uname_ramdisk) {
928 /*
929 * no ramdisk image node unit name, try to get config
930 * node first. If config unit node name is NULL
931 * fit_conf_get_node() will try to find default config node
932 */
Simon Glass0169e6b2012-02-13 13:51:18 +0000933 bootstage_mark(
Simon Glass52823fd2011-12-10 11:08:02 +0000934 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000935 cfg_noffset = fit_conf_get_node(fit_hdr,
936 fit_uname_config);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100937 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000938 puts("Could not find configuration "
939 "node\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000940 bootstage_error(
Simon Glass52823fd2011-12-10 11:08:02 +0000941 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
Michal Simekb2474002008-07-11 10:43:13 +0200942 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100943 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000944 fit_uname_config = fdt_get_name(fit_hdr,
945 cfg_noffset, NULL);
946 printf(" Using '%s' configuration\n",
947 fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100948
Stephen Warren6904b6e2011-10-18 11:11:49 +0000949 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
950 cfg_noffset);
951 fit_uname_ramdisk = fit_get_name(fit_hdr,
952 rd_noffset, NULL);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100953 } else {
954 /* get ramdisk component image node offset */
Simon Glass0169e6b2012-02-13 13:51:18 +0000955 bootstage_mark(
Simon Glass52823fd2011-12-10 11:08:02 +0000956 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000957 rd_noffset = fit_image_get_node(fit_hdr,
958 fit_uname_ramdisk);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100959 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100960 if (rd_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000961 puts("Could not find subimage node\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000962 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
Michal Simekb2474002008-07-11 10:43:13 +0200963 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100964 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100965
Stephen Warren6904b6e2011-10-18 11:11:49 +0000966 printf(" Trying '%s' ramdisk subimage\n",
967 fit_uname_ramdisk);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100968
Simon Glass0169e6b2012-02-13 13:51:18 +0000969 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000970 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
971 images->verify))
Michal Simekb2474002008-07-11 10:43:13 +0200972 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100973
974 /* get ramdisk image data address and length */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000975 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
976 &size)) {
977 puts("Could not find ramdisk subimage data!\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000978 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
Michal Simekb2474002008-07-11 10:43:13 +0200979 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100980 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000981 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100982
983 rd_data = (ulong)data;
984 rd_len = size;
985
Stephen Warren6904b6e2011-10-18 11:11:49 +0000986 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
987 puts("Can't get ramdisk subimage load "
988 "address!\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000989 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
Michal Simekb2474002008-07-11 10:43:13 +0200990 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100991 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000992 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100993
994 images->fit_hdr_rd = fit_hdr;
995 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100996 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100997 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100998#endif
999 default:
Marek Vasut28850d02012-03-18 11:47:58 +00001000#ifdef CONFIG_SUPPORT_RAW_INITRD
1001 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1002 rd_len = simple_strtoul(++end, NULL, 16);
1003 rd_data = rd_addr;
1004 } else
1005#endif
1006 {
1007 puts("Wrong Ramdisk Image Format\n");
1008 rd_data = rd_len = rd_load = 0;
1009 return 1;
1010 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001011 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001012 } else if (images->legacy_hdr_valid &&
Stephen Warren6904b6e2011-10-18 11:11:49 +00001013 image_check_type(&images->legacy_hdr_os_copy,
1014 IH_TYPE_MULTI)) {
1015
Marian Balakowicz95418502008-01-31 13:55:39 +01001016 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001017 * Now check if we have a legacy mult-component image,
1018 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +01001019 */
Simon Glass0169e6b2012-02-13 13:51:18 +00001020 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001021 printf("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +01001022 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001023 (ulong)images->legacy_hdr_os);
1024
Stephen Warren6904b6e2011-10-18 11:11:49 +00001025 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +01001026 } else {
1027 /*
1028 * no initrd image
1029 */
Simon Glass0169e6b2012-02-13 13:51:18 +00001030 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz95418502008-01-31 13:55:39 +01001031 rd_len = rd_data = 0;
1032 }
1033
1034 if (!rd_data) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001035 debug("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +01001036 } else {
1037 *rd_start = rd_data;
1038 *rd_end = rd_data + rd_len;
1039 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001040 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz95418502008-01-31 13:55:39 +01001041 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -06001042
1043 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +01001044}
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001045
John Rigbyeea8e692010-10-13 13:57:35 -06001046#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001047/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001048 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -06001049 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001050 * @rd_data: ramdisk data start address
1051 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001052 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1053 * start address (after possible relocation)
1054 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1055 * end address (after possible relocation)
1056 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001057 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001058 * variable and if requested ramdisk data is moved to a specified location.
1059 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001060 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1061 * start/end addresses if ramdisk image start and len were provided,
1062 * otherwise set initrd_start and initrd_end set to zeros.
1063 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001064 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001065 * 0 - success
1066 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001067 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001068int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001069 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001070{
1071 char *s;
1072 ulong initrd_high;
1073 int initrd_copy_to_ram = 1;
1074
Stephen Warren6904b6e2011-10-18 11:11:49 +00001075 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001076 /* a value of "no" or a similar string will act like 0,
1077 * turning the "load high" feature off. This is intentional.
1078 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001079 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001080 if (initrd_high == ~0)
1081 initrd_copy_to_ram = 0;
1082 } else {
1083 /* not set, no restrictions to load high */
1084 initrd_high = ~0;
1085 }
1086
Marian Balakowicz902ce702008-05-13 15:53:29 +02001087
1088#ifdef CONFIG_LOGBUFFER
1089 /* Prevent initrd from overwriting logbuffer */
1090 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1091#endif
1092
Stephen Warren6904b6e2011-10-18 11:11:49 +00001093 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001094 initrd_high, initrd_copy_to_ram);
1095
1096 if (rd_data) {
1097 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001098 debug(" in-place initrd\n");
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001099 *initrd_start = rd_data;
1100 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001101 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001102 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001103 if (initrd_high)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001104 *initrd_start = (ulong)lmb_alloc_base(lmb,
1105 rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001106 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001107 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1108 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001109
Kumar Galab937bb72008-02-27 21:51:49 -06001110 if (*initrd_start == 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001111 puts("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001112 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001113 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001114 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001115
1116 *initrd_end = *initrd_start + rd_len;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001117 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001118 *initrd_start, *initrd_end);
1119
Stephen Warren6904b6e2011-10-18 11:11:49 +00001120 memmove_wd((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001121 (void *)rd_data, rd_len, CHUNKSZ);
1122
Kumar Gala180c5812011-12-07 04:42:58 +00001123#ifdef CONFIG_MP
1124 /*
1125 * Ensure the image is flushed to memory to handle
1126 * AMP boot scenarios in which we might not be
1127 * HW cache coherent
1128 */
1129 flush_cache((unsigned long)*initrd_start, rd_len);
1130#endif
Stephen Warren6904b6e2011-10-18 11:11:49 +00001131 puts("OK\n");
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001132 }
1133 } else {
1134 *initrd_start = 0;
1135 *initrd_end = 0;
1136 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001137 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001138 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001139
Kumar Galab937bb72008-02-27 21:51:49 -06001140 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001141
Kumar Galab937bb72008-02-27 21:51:49 -06001142error:
1143 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001144}
John Rigbyeea8e692010-10-13 13:57:35 -06001145#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001146
Kumar Galaa5130ad2008-08-15 08:24:38 -05001147#ifdef CONFIG_OF_LIBFDT
Stephen Warren6904b6e2011-10-18 11:11:49 +00001148static void fdt_error(const char *msg)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001149{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001150 puts("ERROR: ");
1151 puts(msg);
1152 puts(" - must RESET the board to recover.\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001153}
1154
Stephen Warren6904b6e2011-10-18 11:11:49 +00001155static const image_header_t *image_get_fdt(ulong fdt_addr)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001156{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001157 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001158
Stephen Warren6904b6e2011-10-18 11:11:49 +00001159 image_print_contents(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001160
Stephen Warren6904b6e2011-10-18 11:11:49 +00001161 puts(" Verifying Checksum ... ");
1162 if (!image_check_hcrc(fdt_hdr)) {
1163 fdt_error("fdt header checksum invalid");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001164 return NULL;
1165 }
1166
Stephen Warren6904b6e2011-10-18 11:11:49 +00001167 if (!image_check_dcrc(fdt_hdr)) {
1168 fdt_error("fdt checksum invalid");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001169 return NULL;
1170 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001171 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001172
Stephen Warren6904b6e2011-10-18 11:11:49 +00001173 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1174 fdt_error("uImage is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001175 return NULL;
1176 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001177 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1178 fdt_error("uImage is compressed");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001179 return NULL;
1180 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001181 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1182 fdt_error("uImage data is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001183 return NULL;
1184 }
1185 return fdt_hdr;
1186}
1187
1188/**
1189 * fit_check_fdt - verify FIT format FDT subimage
1190 * @fit_hdr: pointer to the FIT header
1191 * fdt_noffset: FDT subimage node offset within FIT image
1192 * @verify: data CRC verification flag
1193 *
1194 * fit_check_fdt() verifies integrity of the FDT subimage and from
1195 * specified FIT image.
1196 *
1197 * returns:
1198 * 1, on success
1199 * 0, on failure
1200 */
1201#if defined(CONFIG_FIT)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001202static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001203{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001204 fit_image_print(fit, fdt_noffset, " ");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001205
1206 if (verify) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001207 puts(" Verifying Hash Integrity ... ");
1208 if (!fit_image_check_hashes(fit, fdt_noffset)) {
1209 fdt_error("Bad Data Hash");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001210 return 0;
1211 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001212 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001213 }
1214
Stephen Warren6904b6e2011-10-18 11:11:49 +00001215 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1216 fdt_error("Not a FDT image");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001217 return 0;
1218 }
1219
Stephen Warren6904b6e2011-10-18 11:11:49 +00001220 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1221 fdt_error("FDT image is compressed");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001222 return 0;
1223 }
1224
1225 return 1;
1226}
1227#endif /* CONFIG_FIT */
1228
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001229#ifndef CONFIG_SYS_FDT_PAD
1230#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Galaa5130ad2008-08-15 08:24:38 -05001231#endif
1232
Grant Likely20f54922011-03-28 09:59:01 +00001233#if defined(CONFIG_OF_LIBFDT)
1234/**
1235 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1236 * @lmb: pointer to lmb handle, will be used for memory mgmt
1237 * @fdt_blob: pointer to fdt blob base address
1238 *
1239 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1240 * memreserve regions prevents u-boot from using them to store the initrd
1241 * or the fdt blob.
1242 */
1243void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1244{
1245 uint64_t addr, size;
1246 int i, total;
1247
Stephen Warren6904b6e2011-10-18 11:11:49 +00001248 if (fdt_check_header(fdt_blob) != 0)
Grant Likely20f54922011-03-28 09:59:01 +00001249 return;
1250
1251 total = fdt_num_mem_rsv(fdt_blob);
1252 for (i = 0; i < total; i++) {
1253 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1254 continue;
1255 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1256 (unsigned long long)addr, (unsigned long long)size);
1257 lmb_reserve(lmb, addr, size);
1258 }
1259}
1260
Kumar Galaa5130ad2008-08-15 08:24:38 -05001261/**
1262 * boot_relocate_fdt - relocate flat device tree
1263 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Galaa5130ad2008-08-15 08:24:38 -05001264 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1265 * @of_size: pointer to a ulong variable, will hold fdt length
1266 *
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001267 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1268 * relocates the of_flat_tree into that region, even if the fdt is already in
1269 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1270 * bytes.
Kumar Galaa5130ad2008-08-15 08:24:38 -05001271 *
1272 * of_flat_tree and of_size are set to final (after relocation) values
1273 *
1274 * returns:
1275 * 0 - success
1276 * 1 - failure
1277 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001278int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001279{
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001280 void *fdt_blob = *of_flat_tree;
1281 void *of_start = 0;
David A. Longd558a4e2011-07-09 16:40:19 -04001282 char *fdt_high;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001283 ulong of_len = 0;
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001284 int err;
David A. Longd558a4e2011-07-09 16:40:19 -04001285 int disable_relocation = 0;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001286
1287 /* nothing to do */
1288 if (*of_size == 0)
1289 return 0;
1290
Stephen Warren6904b6e2011-10-18 11:11:49 +00001291 if (fdt_check_header(fdt_blob) != 0) {
1292 fdt_error("image is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001293 goto error;
1294 }
1295
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001296 /* position on a 4K boundary before the alloc_current */
1297 /* Pad the FDT by a specified amount */
1298 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longd558a4e2011-07-09 16:40:19 -04001299
1300 /* If fdt_high is set use it to select the relocation address */
1301 fdt_high = getenv("fdt_high");
1302 if (fdt_high) {
1303 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1304
1305 if (((ulong) desired_addr) == ~0UL) {
1306 /* All ones means use fdt in place */
Shawn Guo0ca9e982012-01-09 21:54:08 +00001307 of_start = fdt_blob;
1308 lmb_reserve(lmb, (ulong)of_start, of_len);
David A. Longd558a4e2011-07-09 16:40:19 -04001309 disable_relocation = 1;
Shawn Guo0ca9e982012-01-09 21:54:08 +00001310 } else if (desired_addr) {
David A. Longd558a4e2011-07-09 16:40:19 -04001311 of_start =
1312 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
Shawn Guo0ca9e982012-01-09 21:54:08 +00001313 (ulong)desired_addr);
1314 if (of_start == 0) {
David A. Longd558a4e2011-07-09 16:40:19 -04001315 puts("Failed using fdt_high value for Device Tree");
1316 goto error;
1317 }
1318 } else {
1319 of_start =
Matthew McClintockdcf6fd72011-07-18 13:08:05 +00001320 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longd558a4e2011-07-09 16:40:19 -04001321 }
1322 } else {
1323 of_start =
1324 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1325 getenv_bootm_mapsize()
1326 + getenv_bootm_low());
1327 }
Kumar Galaa5130ad2008-08-15 08:24:38 -05001328
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001329 if (of_start == 0) {
1330 puts("device tree - allocation error\n");
1331 goto error;
1332 }
Kumar Galaa5130ad2008-08-15 08:24:38 -05001333
David A. Longd558a4e2011-07-09 16:40:19 -04001334 if (disable_relocation) {
1335 /* We assume there is space after the existing fdt to use for padding */
1336 fdt_set_totalsize(of_start, of_len);
1337 printf(" Using Device Tree in place at %p, end %p\n",
1338 of_start, of_start + of_len - 1);
1339 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001340 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
David A. Longd558a4e2011-07-09 16:40:19 -04001341 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001342
Stephen Warren6904b6e2011-10-18 11:11:49 +00001343 printf(" Loading Device Tree to %p, end %p ... ",
David A. Longd558a4e2011-07-09 16:40:19 -04001344 of_start, of_start + of_len - 1);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001345
Stephen Warren6904b6e2011-10-18 11:11:49 +00001346 err = fdt_open_into(fdt_blob, of_start, of_len);
David A. Longd558a4e2011-07-09 16:40:19 -04001347 if (err != 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001348 fdt_error("fdt move failed");
David A. Longd558a4e2011-07-09 16:40:19 -04001349 goto error;
1350 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001351 puts("OK\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001352 }
Timur Tabi6b8f2f82010-05-24 15:10:25 -05001353
1354 *of_flat_tree = of_start;
1355 *of_size = of_len;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001356
Kumar Gala9f4e7e42008-08-15 08:24:39 -05001357 set_working_fdt_addr(*of_flat_tree);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001358 return 0;
1359
1360error:
1361 return 1;
1362}
Grant Likely50fb22e2011-03-28 09:58:49 +00001363#endif /* CONFIG_OF_LIBFDT */
Kumar Galaa5130ad2008-08-15 08:24:38 -05001364
1365/**
1366 * boot_get_fdt - main fdt handling routine
1367 * @argc: command argument count
1368 * @argv: command argument list
1369 * @images: pointer to the bootm images structure
1370 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1371 * @of_size: pointer to a ulong variable, will hold fdt length
1372 *
1373 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1374 * Curently supported are the following ramdisk sources:
1375 * - multicomponent kernel/ramdisk image,
1376 * - commandline provided address of decicated ramdisk image.
1377 *
1378 * returns:
1379 * 0, if fdt image was found and valid, or skipped
1380 * of_flat_tree and of_size are set to fdt start address and length if
1381 * fdt image is found and valid
1382 *
1383 * 1, if fdt image is found but corrupted
1384 * of_flat_tree and of_size are set to 0 if no fdt exists
1385 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001386int boot_get_fdt(int flag, int argc, char * const argv[],
1387 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001388{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001389 const image_header_t *fdt_hdr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001390 ulong fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001391 char *fdt_blob = NULL;
Stephen Warren84d62282011-11-01 06:28:21 +00001392 ulong image_start, image_data, image_end;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001393 ulong load_start, load_end;
1394#if defined(CONFIG_FIT)
1395 void *fit_hdr;
1396 const char *fit_uname_config = NULL;
1397 const char *fit_uname_fdt = NULL;
1398 ulong default_addr;
1399 int cfg_noffset;
1400 int fdt_noffset;
1401 const void *data;
1402 size_t size;
1403#endif
1404
1405 *of_flat_tree = NULL;
1406 *of_size = 0;
1407
Stephen Warren6904b6e2011-10-18 11:11:49 +00001408 if (argc > 3 || genimg_has_config(images)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001409#if defined(CONFIG_FIT)
1410 if (argc > 3) {
1411 /*
1412 * If the FDT blob comes from the FIT image and the
1413 * FIT image address is omitted in the command line
1414 * argument, try to use ramdisk or os FIT image
1415 * address or default load address.
1416 */
1417 if (images->fit_uname_rd)
1418 default_addr = (ulong)images->fit_hdr_rd;
1419 else if (images->fit_uname_os)
1420 default_addr = (ulong)images->fit_hdr_os;
1421 else
1422 default_addr = load_addr;
1423
Stephen Warren6904b6e2011-10-18 11:11:49 +00001424 if (fit_parse_conf(argv[3], default_addr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001425 &fdt_addr, &fit_uname_config)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001426 debug("* fdt: config '%s' from image at "
1427 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001428 fit_uname_config, fdt_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001429 } else if (fit_parse_subimage(argv[3], default_addr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001430 &fdt_addr, &fit_uname_fdt)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001431 debug("* fdt: subimage '%s' from image at "
1432 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001433 fit_uname_fdt, fdt_addr);
1434 } else
1435#endif
1436 {
1437 fdt_addr = simple_strtoul(argv[3], NULL, 16);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001438 debug("* fdt: cmdline image address = "
1439 "0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001440 fdt_addr);
1441 }
1442#if defined(CONFIG_FIT)
1443 } else {
1444 /* use FIT configuration provided in first bootm
1445 * command argument
1446 */
1447 fdt_addr = (ulong)images->fit_hdr_os;
1448 fit_uname_config = images->fit_uname_cfg;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001449 debug("* fdt: using config '%s' from image "
1450 "at 0x%08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001451 fit_uname_config, fdt_addr);
1452
1453 /*
1454 * Check whether configuration has FDT blob defined,
1455 * if not quit silently.
1456 */
1457 fit_hdr = (void *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001458 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001459 fit_uname_config);
1460 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001461 debug("* fdt: no such config\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001462 return 0;
1463 }
1464
Stephen Warren6904b6e2011-10-18 11:11:49 +00001465 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001466 cfg_noffset);
1467 if (fdt_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001468 debug("* fdt: no fdt in config\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001469 return 0;
1470 }
1471 }
1472#endif
1473
Stephen Warren6904b6e2011-10-18 11:11:49 +00001474 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001475 fdt_addr);
1476
1477 /* copy from dataflash if needed */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001478 fdt_addr = genimg_get_image(fdt_addr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001479
1480 /*
1481 * Check if there is an FDT image at the
1482 * address provided in the second bootm argument
1483 * check image type, for FIT images get a FIT node.
1484 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001485 switch (genimg_get_format((void *)fdt_addr)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001486 case IMAGE_FORMAT_LEGACY:
1487 /* verify fdt_addr points to a valid image header */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001488 printf("## Flattened Device Tree from Legacy Image "
1489 "at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001490 fdt_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001491 fdt_hdr = image_get_fdt(fdt_addr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001492 if (!fdt_hdr)
1493 goto error;
1494
1495 /*
1496 * move image data to the load address,
1497 * make sure we don't overwrite initial image
1498 */
1499 image_start = (ulong)fdt_hdr;
Stephen Warren84d62282011-11-01 06:28:21 +00001500 image_data = (ulong)image_get_data(fdt_hdr);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001501 image_end = image_get_image_end(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001502
Stephen Warren6904b6e2011-10-18 11:11:49 +00001503 load_start = image_get_load(fdt_hdr);
1504 load_end = load_start + image_get_data_size(fdt_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001505
Stephen Warren84d62282011-11-01 06:28:21 +00001506 if (load_start == image_start ||
1507 load_start == image_data) {
1508 fdt_blob = (char *)image_data;
1509 break;
1510 }
1511
Kumar Galaa5130ad2008-08-15 08:24:38 -05001512 if ((load_start < image_end) && (load_end > image_start)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001513 fdt_error("fdt overwritten");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001514 goto error;
1515 }
1516
Stephen Warren6904b6e2011-10-18 11:11:49 +00001517 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
Stephen Warren84d62282011-11-01 06:28:21 +00001518 image_data, load_start);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001519
Stephen Warren6904b6e2011-10-18 11:11:49 +00001520 memmove((void *)load_start,
Stephen Warren84d62282011-11-01 06:28:21 +00001521 (void *)image_data,
Stephen Warren6904b6e2011-10-18 11:11:49 +00001522 image_get_data_size(fdt_hdr));
Kumar Galaa5130ad2008-08-15 08:24:38 -05001523
1524 fdt_blob = (char *)load_start;
1525 break;
1526 case IMAGE_FORMAT_FIT:
1527 /*
1528 * This case will catch both: new uImage format
1529 * (libfdt based) and raw FDT blob (also libfdt
1530 * based).
1531 */
1532#if defined(CONFIG_FIT)
1533 /* check FDT blob vs FIT blob */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001534 if (fit_check_format((const void *)fdt_addr)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001535 /*
1536 * FIT image
1537 */
1538 fit_hdr = (void *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001539 printf("## Flattened Device Tree from FIT "
1540 "Image at %08lx\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001541 fdt_addr);
1542
1543 if (!fit_uname_fdt) {
1544 /*
1545 * no FDT blob image node unit name,
1546 * try to get config node first. If
1547 * config unit node name is NULL
1548 * fit_conf_get_node() will try to
1549 * find default config node
1550 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001551 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001552 fit_uname_config);
1553
1554 if (cfg_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001555 fdt_error("Could not find "
1556 "configuration "
1557 "node\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001558 goto error;
1559 }
1560
Stephen Warren6904b6e2011-10-18 11:11:49 +00001561 fit_uname_config = fdt_get_name(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001562 cfg_noffset, NULL);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001563 printf(" Using '%s' configuration\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001564 fit_uname_config);
1565
Stephen Warren6904b6e2011-10-18 11:11:49 +00001566 fdt_noffset = fit_conf_get_fdt_node(
1567 fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001568 cfg_noffset);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001569 fit_uname_fdt = fit_get_name(fit_hdr,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001570 fdt_noffset, NULL);
1571 } else {
1572 /* get FDT component image node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001573 fdt_noffset = fit_image_get_node(
1574 fit_hdr,
1575 fit_uname_fdt);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001576 }
1577 if (fdt_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001578 fdt_error("Could not find subimage "
1579 "node\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001580 goto error;
1581 }
1582
Stephen Warren6904b6e2011-10-18 11:11:49 +00001583 printf(" Trying '%s' FDT blob subimage\n",
Kumar Galaa5130ad2008-08-15 08:24:38 -05001584 fit_uname_fdt);
1585
Stephen Warren6904b6e2011-10-18 11:11:49 +00001586 if (!fit_check_fdt(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001587 images->verify))
1588 goto error;
1589
1590 /* get ramdisk image data address and length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001591 if (fit_image_get_data(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001592 &data, &size)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001593 fdt_error("Could not find FDT "
1594 "subimage data");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001595 goto error;
1596 }
1597
1598 /* verift that image data is a proper FDT blob */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001599 if (fdt_check_header((char *)data) != 0) {
1600 fdt_error("Subimage data is not a FTD");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001601 goto error;
1602 }
1603
1604 /*
1605 * move image data to the load address,
1606 * make sure we don't overwrite initial image
1607 */
1608 image_start = (ulong)fit_hdr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001609 image_end = fit_get_end(fit_hdr);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001610
Stephen Warren6904b6e2011-10-18 11:11:49 +00001611 if (fit_image_get_load(fit_hdr, fdt_noffset,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001612 &load_start) == 0) {
1613 load_end = load_start + size;
1614
1615 if ((load_start < image_end) &&
1616 (load_end > image_start)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001617 fdt_error("FDT overwritten");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001618 goto error;
1619 }
1620
Stephen Warren6904b6e2011-10-18 11:11:49 +00001621 printf(" Loading FDT from 0x%08lx "
1622 "to 0x%08lx\n",
1623 (ulong)data,
1624 load_start);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001625
Stephen Warren6904b6e2011-10-18 11:11:49 +00001626 memmove((void *)load_start,
Kumar Galaa5130ad2008-08-15 08:24:38 -05001627 (void *)data, size);
1628
1629 fdt_blob = (char *)load_start;
1630 } else {
1631 fdt_blob = (char *)data;
1632 }
1633
1634 images->fit_hdr_fdt = fit_hdr;
1635 images->fit_uname_fdt = fit_uname_fdt;
1636 images->fit_noffset_fdt = fdt_noffset;
1637 break;
1638 } else
1639#endif
1640 {
1641 /*
1642 * FDT blob
1643 */
1644 fdt_blob = (char *)fdt_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001645 debug("* fdt: raw FDT blob\n");
1646 printf("## Flattened Device Tree blob at "
1647 "%08lx\n", (long)fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001648 }
1649 break;
1650 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001651 puts("ERROR: Did not find a cmdline Flattened Device "
1652 "Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001653 goto error;
1654 }
1655
Simon Glass00aa9882011-09-23 06:22:04 +00001656 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001657
1658 } else if (images->legacy_hdr_valid &&
Stephen Warren6904b6e2011-10-18 11:11:49 +00001659 image_check_type(&images->legacy_hdr_os_copy,
1660 IH_TYPE_MULTI)) {
Kumar Galaa5130ad2008-08-15 08:24:38 -05001661
1662 ulong fdt_data, fdt_len;
1663
1664 /*
1665 * Now check if we have a legacy multi-component image,
1666 * get second entry data start address and len.
1667 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001668 printf("## Flattened Device Tree from multi "
Kumar Galaa5130ad2008-08-15 08:24:38 -05001669 "component Image at %08lX\n",
1670 (ulong)images->legacy_hdr_os);
1671
Stephen Warren6904b6e2011-10-18 11:11:49 +00001672 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1673 &fdt_len);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001674 if (fdt_len) {
1675
1676 fdt_blob = (char *)fdt_data;
Simon Glass00aa9882011-09-23 06:22:04 +00001677 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001678
Stephen Warren6904b6e2011-10-18 11:11:49 +00001679 if (fdt_check_header(fdt_blob) != 0) {
1680 fdt_error("image is not a fdt");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001681 goto error;
1682 }
1683
John Rigbyb763f932010-10-13 13:57:32 -06001684 if (fdt_totalsize(fdt_blob) != fdt_len) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001685 fdt_error("fdt size != image size");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001686 goto error;
1687 }
1688 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001689 debug("## No Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001690 return 0;
1691 }
1692 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001693 debug("## No Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001694 return 0;
1695 }
1696
1697 *of_flat_tree = fdt_blob;
John Rigbyb763f932010-10-13 13:57:32 -06001698 *of_size = fdt_totalsize(fdt_blob);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001699 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner7ddfafc2008-08-21 07:12:26 -07001700 (ulong)*of_flat_tree, *of_size);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001701
1702 return 0;
1703
1704error:
1705 *of_flat_tree = 0;
1706 *of_size = 0;
1707 return 1;
1708}
1709#endif /* CONFIG_OF_LIBFDT */
1710
John Rigbyeea8e692010-10-13 13:57:35 -06001711#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001712/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001713 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001714 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001715 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1716 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1717 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001718 * boot_get_cmdline() allocates space for kernel command line below
Grant Likelydfff7a22011-03-28 09:58:34 +00001719 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001720 * variable is present its contents is copied to allocated kernel
1721 * command line.
1722 *
1723 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001724 * 0 - success
1725 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001726 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001727int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001728{
1729 char *cmdline;
1730 char *s;
1731
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001732 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likely26396382011-03-28 09:58:43 +00001733 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galab937bb72008-02-27 21:51:49 -06001734
1735 if (cmdline == NULL)
1736 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001737
1738 if ((s = getenv("bootargs")) == NULL)
1739 s = "";
1740
1741 strcpy(cmdline, s);
1742
1743 *cmd_start = (ulong) & cmdline[0];
1744 *cmd_end = *cmd_start + strlen(cmdline);
1745
Stephen Warren6904b6e2011-10-18 11:11:49 +00001746 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001747
Kumar Galab937bb72008-02-27 21:51:49 -06001748 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001749}
John Rigbyeea8e692010-10-13 13:57:35 -06001750#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001751
John Rigbyeea8e692010-10-13 13:57:35 -06001752#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001753/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001754 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001755 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001756 * @kbd: double pointer to board info data
1757 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001758 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likelydfff7a22011-03-28 09:58:34 +00001759 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1760 * with the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001761 *
1762 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001763 * 0 - success
1764 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001765 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001766int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001767{
Becky Bruced26d67c2008-06-09 20:37:18 -05001768 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likely26396382011-03-28 09:58:43 +00001769 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galab937bb72008-02-27 21:51:49 -06001770 if (*kbd == NULL)
1771 return -1;
1772
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001773 **kbd = *(gd->bd);
1774
Stephen Warren6904b6e2011-10-18 11:11:49 +00001775 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001776
1777#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1778 do_bdinfo(NULL, 0, 0, NULL);
1779#endif
1780
Kumar Galab937bb72008-02-27 21:51:49 -06001781 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001782}
John Rigbyeea8e692010-10-13 13:57:35 -06001783#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001784#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001785
Marian Balakowicz3413e222008-02-21 17:20:20 +01001786#if defined(CONFIG_FIT)
1787/*****************************************************************************/
1788/* New uImage format routines */
1789/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001790#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00001791static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001792 ulong *addr, const char **name)
1793{
1794 const char *sep;
1795
1796 *addr = addr_curr;
1797 *name = NULL;
1798
Stephen Warren6904b6e2011-10-18 11:11:49 +00001799 sep = strchr(spec, sepc);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001800 if (sep) {
1801 if (sep - spec > 0)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001802 *addr = simple_strtoul(spec, NULL, 16);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001803
1804 *name = sep + 1;
1805 return 1;
1806 }
1807
1808 return 0;
1809}
1810
1811/**
1812 * fit_parse_conf - parse FIT configuration spec
1813 * @spec: input string, containing configuration spec
1814 * @add_curr: current image address (to be used as a possible default)
1815 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1816 * configuration
1817 * @conf_name double pointer to a char, will hold pointer to a configuration
1818 * unit name
1819 *
1820 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1821 * where <addr> is a FIT image address that contains configuration
1822 * with a <conf> unit name.
1823 *
1824 * Address part is optional, and if omitted default add_curr will
1825 * be used instead.
1826 *
1827 * returns:
1828 * 1 if spec is a valid configuration string,
1829 * addr and conf_name are set accordingly
1830 * 0 otherwise
1831 */
Mike Frysinger711453d2012-04-22 06:59:06 +00001832int fit_parse_conf(const char *spec, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001833 ulong *addr, const char **conf_name)
1834{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001835 return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001836}
1837
1838/**
1839 * fit_parse_subimage - parse FIT subimage spec
1840 * @spec: input string, containing subimage spec
1841 * @add_curr: current image address (to be used as a possible default)
1842 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1843 * subimage
1844 * @image_name: double pointer to a char, will hold pointer to a subimage name
1845 *
1846 * fit_parse_subimage() expects subimage spec in the for of
1847 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1848 * subimage with a <subimg> unit name.
1849 *
1850 * Address part is optional, and if omitted default add_curr will
1851 * be used instead.
1852 *
1853 * returns:
1854 * 1 if spec is a valid subimage string,
1855 * addr and image_name are set accordingly
1856 * 0 otherwise
1857 */
Mike Frysinger711453d2012-04-22 06:59:06 +00001858int fit_parse_subimage(const char *spec, ulong addr_curr,
Marian Balakowicz3413e222008-02-21 17:20:20 +01001859 ulong *addr, const char **image_name)
1860{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001861 return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
Marian Balakowicz3413e222008-02-21 17:20:20 +01001862}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001863#endif /* !USE_HOSTCC */
1864
Stephen Warren6904b6e2011-10-18 11:11:49 +00001865static void fit_get_debug(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001866 char *prop_name, int err)
1867{
Stephen Warren6904b6e2011-10-18 11:11:49 +00001868 debug("Can't get '%s' property from FIT 0x%08lx, "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001869 "node: offset %d, name %s (%s)\n",
1870 prop_name, (ulong)fit, noffset,
Stephen Warren6904b6e2011-10-18 11:11:49 +00001871 fit_get_name(fit, noffset, NULL),
1872 fdt_strerror(err));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001873}
1874
1875/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001876 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001877 * @fit: pointer to the FIT format image header
1878 * @p: pointer to prefix string
1879 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001880 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001881 * The routine prints out FIT image properties (root node level) follwed by
1882 * the details of each component image.
1883 *
1884 * returns:
1885 * no returned results
1886 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001887void fit_print_contents(const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001888{
1889 char *desc;
1890 char *uname;
1891 int images_noffset;
1892 int confs_noffset;
1893 int noffset;
1894 int ndepth;
1895 int count = 0;
1896 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001897 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001898#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1899 time_t timestamp;
1900#endif
1901
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001902#ifdef USE_HOSTCC
1903 p = "";
1904#else
1905 p = " ";
1906#endif
1907
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001908 /* Root node properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001909 ret = fit_get_desc(fit, 0, &desc);
1910 printf("%sFIT description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001911 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001912 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001913 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001914 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001915
1916#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001917 ret = fit_get_timestamp(fit, 0, &timestamp);
1918 printf("%sCreated: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001919 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001920 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001921 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001922 genimg_print_time(timestamp);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001923#endif
1924
1925 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001926 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001927 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001928 printf("Can't find images parent node '%s' (%s)\n",
1929 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001930 return;
1931 }
1932
1933 /* Process its subnodes, print out component images details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001934 for (ndepth = 0, count = 0,
1935 noffset = fdt_next_node(fit, images_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 images parent node,
1941 * i.e. component image node.
1942 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001943 printf("%s Image %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_image_print(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001947 }
1948 }
1949
1950 /* Find configurations parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001951 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001952 if (confs_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001953 debug("Can't get configurations parent node '%s' (%s)\n",
1954 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001955 return;
1956 }
1957
1958 /* get default configuration unit name from default property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001959 uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001960 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001961 printf("%s Default Configuration: '%s'\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001962
1963 /* Process its subnodes, print out configurations details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001964 for (ndepth = 0, count = 0,
1965 noffset = fdt_next_node(fit, confs_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001966 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001967 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001968 if (ndepth == 1) {
1969 /*
1970 * Direct child node of the configurations parent node,
1971 * i.e. configuration node.
1972 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001973 printf("%s Configuration %u (%s)\n", p, count++,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001974 fit_get_name(fit, noffset, NULL));
1975
Stephen Warren6904b6e2011-10-18 11:11:49 +00001976 fit_conf_print(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001977 }
1978 }
1979}
1980
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001981/**
1982 * fit_image_print - prints out the FIT component image details
1983 * @fit: pointer to the FIT format image header
1984 * @image_noffset: offset of the component image node
1985 * @p: pointer to prefix string
1986 *
1987 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001988 * image. If present, hash nodes are printed out as well. Load
1989 * address for images of type firmware is also printed out. Since the load
1990 * address is not mandatory for firmware images, it will be output as
1991 * "unavailable" when not present.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001992 *
1993 * returns:
1994 * no returned results
1995 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001996void fit_image_print(const void *fit, int image_noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001997{
1998 char *desc;
1999 uint8_t type, arch, os, comp;
2000 size_t size;
2001 ulong load, entry;
2002 const void *data;
2003 int noffset;
2004 int ndepth;
2005 int ret;
2006
2007 /* Mandatory properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002008 ret = fit_get_desc(fit, image_noffset, &desc);
2009 printf("%s Description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002010 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002011 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002012 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002013 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002014
Stephen Warren6904b6e2011-10-18 11:11:49 +00002015 fit_image_get_type(fit, image_noffset, &type);
2016 printf("%s Type: %s\n", p, genimg_get_type_name(type));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002017
Stephen Warren6904b6e2011-10-18 11:11:49 +00002018 fit_image_get_comp(fit, image_noffset, &comp);
2019 printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002020
Stephen Warren6904b6e2011-10-18 11:11:49 +00002021 ret = fit_image_get_data(fit, image_noffset, &data, &size);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002022
2023#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00002024 printf("%s Data Start: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002025 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002026 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002027 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002028 printf("0x%08lx\n", (ulong)data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002029#endif
2030
Stephen Warren6904b6e2011-10-18 11:11:49 +00002031 printf("%s Data Size: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002032 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002033 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002034 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002035 genimg_print_size(size);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002036
2037 /* Remaining, type dependent properties */
2038 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2039 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
2040 (type == IH_TYPE_FLATDT)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002041 fit_image_get_arch(fit, image_noffset, &arch);
2042 printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002043 }
2044
2045 if (type == IH_TYPE_KERNEL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002046 fit_image_get_os(fit, image_noffset, &os);
2047 printf("%s OS: %s\n", p, genimg_get_os_name(os));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002048 }
2049
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002050 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
2051 (type == IH_TYPE_FIRMWARE)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002052 ret = fit_image_get_load(fit, image_noffset, &load);
2053 printf("%s Load Address: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002054 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002055 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002056 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002057 printf("0x%08lx\n", load);
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002058 }
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002059
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002060 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002061 fit_image_get_entry(fit, image_noffset, &entry);
2062 printf("%s Entry Point: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002063 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002064 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002065 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00002066 printf("0x%08lx\n", entry);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002067 }
2068
2069 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002070 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002071 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002072 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002073 if (ndepth == 1) {
2074 /* Direct child node of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002075 fit_image_print_hash(fit, noffset, p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002076 }
2077 }
2078}
2079
2080/**
2081 * fit_image_print_hash - prints out the hash node details
2082 * @fit: pointer to the FIT format image header
2083 * @noffset: offset of the hash node
2084 * @p: pointer to prefix string
2085 *
2086 * fit_image_print_hash() lists properies for the processed hash node
2087 *
2088 * returns:
2089 * no returned results
2090 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002091void fit_image_print_hash(const void *fit, int noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002092{
2093 char *algo;
2094 uint8_t *value;
2095 int value_len;
2096 int i, ret;
2097
2098 /*
2099 * Check subnode name, must be equal to "hash".
2100 * Multiple hash nodes require unique unit node
2101 * names, e.g. hash@1, hash@2, etc.
2102 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002103 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002104 FIT_HASH_NODENAME,
2105 strlen(FIT_HASH_NODENAME)) != 0)
2106 return;
2107
Stephen Warren6904b6e2011-10-18 11:11:49 +00002108 debug("%s Hash node: '%s'\n", p,
2109 fit_get_name(fit, noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002110
Stephen Warren6904b6e2011-10-18 11:11:49 +00002111 printf("%s Hash algo: ", p);
2112 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2113 printf("invalid/unsupported\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002114 return;
2115 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002116 printf("%s\n", algo);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002117
Stephen Warren6904b6e2011-10-18 11:11:49 +00002118 ret = fit_image_hash_get_value(fit, noffset, &value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002119 &value_len);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002120 printf("%s Hash value: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002121 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002122 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002123 } else {
2124 for (i = 0; i < value_len; i++)
Stephen Warren6904b6e2011-10-18 11:11:49 +00002125 printf("%02x", value[i]);
2126 printf("\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002127 }
2128
Stephen Warren6904b6e2011-10-18 11:11:49 +00002129 debug("%s Hash len: %d\n", p, value_len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002130}
2131
2132/**
2133 * fit_get_desc - get node description property
2134 * @fit: pointer to the FIT format image header
2135 * @noffset: node offset
2136 * @desc: double pointer to the char, will hold pointer to the descrption
2137 *
2138 * fit_get_desc() reads description property from a given node, if
2139 * description is found pointer to it is returened in third call argument.
2140 *
2141 * returns:
2142 * 0, on success
2143 * -1, on failure
2144 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002145int fit_get_desc(const void *fit, int noffset, char **desc)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002146{
2147 int len;
2148
Stephen Warren6904b6e2011-10-18 11:11:49 +00002149 *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002150 if (*desc == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002151 fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002152 return -1;
2153 }
2154
2155 return 0;
2156}
2157
2158/**
2159 * fit_get_timestamp - get node timestamp property
2160 * @fit: pointer to the FIT format image header
2161 * @noffset: node offset
2162 * @timestamp: pointer to the time_t, will hold read timestamp
2163 *
2164 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2165 * is found and has a correct size its value is retured in third call
2166 * argument.
2167 *
2168 * returns:
2169 * 0, on success
2170 * -1, on property read failure
2171 * -2, on wrong timestamp size
2172 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002173int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002174{
2175 int len;
2176 const void *data;
2177
Stephen Warren6904b6e2011-10-18 11:11:49 +00002178 data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002179 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002180 fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002181 return -1;
2182 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002183 if (len != sizeof(uint32_t)) {
2184 debug("FIT timestamp with incorrect size of (%u)\n", len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002185 return -2;
2186 }
2187
Stephen Warren6904b6e2011-10-18 11:11:49 +00002188 *timestamp = uimage_to_cpu(*((uint32_t *)data));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002189 return 0;
2190}
2191
2192/**
2193 * fit_image_get_node - get node offset for component image of a given unit name
2194 * @fit: pointer to the FIT format image header
2195 * @image_uname: component image node unit name
2196 *
2197 * fit_image_get_node() finds a component image (withing the '/images'
2198 * node) of a provided unit name. If image is found its node offset is
2199 * returned to the caller.
2200 *
2201 * returns:
2202 * image node offset when found (>=0)
2203 * negative number on failure (FDT_ERR_* code)
2204 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002205int fit_image_get_node(const void *fit, const char *image_uname)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002206{
2207 int noffset, images_noffset;
2208
Stephen Warren6904b6e2011-10-18 11:11:49 +00002209 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002210 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002211 debug("Can't find images parent node '%s' (%s)\n",
2212 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002213 return images_noffset;
2214 }
2215
Stephen Warren6904b6e2011-10-18 11:11:49 +00002216 noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002217 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002218 debug("Can't get node offset for image unit name: '%s' (%s)\n",
2219 image_uname, fdt_strerror(noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002220 }
2221
2222 return noffset;
2223}
2224
2225/**
2226 * fit_image_get_os - get os id for a given component image node
2227 * @fit: pointer to the FIT format image header
2228 * @noffset: component image node offset
2229 * @os: pointer to the uint8_t, will hold os numeric id
2230 *
2231 * fit_image_get_os() finds os property in a given component image node.
2232 * If the property is found, its (string) value is translated to the numeric
2233 * id which is returned to the caller.
2234 *
2235 * returns:
2236 * 0, on success
2237 * -1, on failure
2238 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002239int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002240{
2241 int len;
2242 const void *data;
2243
2244 /* Get OS name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002245 data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002246 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002247 fit_get_debug(fit, noffset, FIT_OS_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002248 *os = -1;
2249 return -1;
2250 }
2251
2252 /* Translate OS name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002253 *os = genimg_get_os_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002254 return 0;
2255}
2256
2257/**
2258 * fit_image_get_arch - get arch id for a given component image node
2259 * @fit: pointer to the FIT format image header
2260 * @noffset: component image node offset
2261 * @arch: pointer to the uint8_t, will hold arch numeric id
2262 *
2263 * fit_image_get_arch() finds arch property in a given component image node.
2264 * If the property is found, its (string) value is translated to the numeric
2265 * id which is returned to the caller.
2266 *
2267 * returns:
2268 * 0, on success
2269 * -1, on failure
2270 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002271int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002272{
2273 int len;
2274 const void *data;
2275
2276 /* Get architecture name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002277 data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002278 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002279 fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002280 *arch = -1;
2281 return -1;
2282 }
2283
2284 /* Translate architecture name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002285 *arch = genimg_get_arch_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002286 return 0;
2287}
2288
2289/**
2290 * fit_image_get_type - get type id for a given component image node
2291 * @fit: pointer to the FIT format image header
2292 * @noffset: component image node offset
2293 * @type: pointer to the uint8_t, will hold type numeric id
2294 *
2295 * fit_image_get_type() finds type property in a given component image node.
2296 * If the property is found, its (string) value is translated to the numeric
2297 * id which is returned to the caller.
2298 *
2299 * returns:
2300 * 0, on success
2301 * -1, on failure
2302 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002303int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002304{
2305 int len;
2306 const void *data;
2307
2308 /* Get image type name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002309 data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002310 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002311 fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002312 *type = -1;
2313 return -1;
2314 }
2315
2316 /* Translate image type name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002317 *type = genimg_get_type_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002318 return 0;
2319}
2320
2321/**
2322 * fit_image_get_comp - get comp id for a given component image node
2323 * @fit: pointer to the FIT format image header
2324 * @noffset: component image node offset
2325 * @comp: pointer to the uint8_t, will hold comp numeric id
2326 *
2327 * fit_image_get_comp() finds comp property in a given component image node.
2328 * If the property is found, its (string) value is translated to the numeric
2329 * id which is returned to the caller.
2330 *
2331 * returns:
2332 * 0, on success
2333 * -1, on failure
2334 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002335int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002336{
2337 int len;
2338 const void *data;
2339
2340 /* Get compression name from property data */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002341 data = fdt_getprop(fit, noffset, FIT_COMP_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_COMP_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002344 *comp = -1;
2345 return -1;
2346 }
2347
2348 /* Translate compression name to id */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002349 *comp = genimg_get_comp_id(data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002350 return 0;
2351}
2352
2353/**
2354 * fit_image_get_load - get load address property for a given component image node
2355 * @fit: pointer to the FIT format image header
2356 * @noffset: component image node offset
2357 * @load: pointer to the uint32_t, will hold load address
2358 *
2359 * fit_image_get_load() finds load address property in a given component image node.
2360 * If the property is found, its value is returned to the caller.
2361 *
2362 * returns:
2363 * 0, on success
2364 * -1, on failure
2365 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002366int fit_image_get_load(const void *fit, int noffset, ulong *load)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002367{
2368 int len;
2369 const uint32_t *data;
2370
Stephen Warren6904b6e2011-10-18 11:11:49 +00002371 data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002372 if (data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002373 fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002374 return -1;
2375 }
2376
Stephen Warren6904b6e2011-10-18 11:11:49 +00002377 *load = uimage_to_cpu(*data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002378 return 0;
2379}
2380
2381/**
2382 * fit_image_get_entry - get entry point address property for a given component image node
2383 * @fit: pointer to the FIT format image header
2384 * @noffset: component image node offset
2385 * @entry: pointer to the uint32_t, will hold entry point address
2386 *
2387 * fit_image_get_entry() finds entry point address property in a given component image node.
2388 * If the property is found, its value is returned to 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_entry(const void *fit, int noffset, ulong *entry)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002395{
2396 int len;
2397 const uint32_t *data;
2398
Stephen Warren6904b6e2011-10-18 11:11:49 +00002399 data = fdt_getprop(fit, noffset, FIT_ENTRY_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_ENTRY_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002402 return -1;
2403 }
2404
Stephen Warren6904b6e2011-10-18 11:11:49 +00002405 *entry = uimage_to_cpu(*data);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002406 return 0;
2407}
2408
2409/**
2410 * fit_image_get_data - get data property and its size for a given component image node
2411 * @fit: pointer to the FIT format image header
2412 * @noffset: component image node offset
2413 * @data: double pointer to void, will hold data property's data address
2414 * @size: pointer to size_t, will hold data property's data size
2415 *
2416 * fit_image_get_data() finds data property in a given component image node.
2417 * If the property is found its data start address and size are returned to
2418 * the caller.
2419 *
2420 * returns:
2421 * 0, on success
2422 * -1, on failure
2423 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002424int fit_image_get_data(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002425 const void **data, size_t *size)
2426{
2427 int len;
2428
Stephen Warren6904b6e2011-10-18 11:11:49 +00002429 *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002430 if (*data == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002431 fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002432 *size = 0;
2433 return -1;
2434 }
2435
2436 *size = len;
2437 return 0;
2438}
2439
2440/**
2441 * fit_image_hash_get_algo - get hash algorithm name
2442 * @fit: pointer to the FIT format image header
2443 * @noffset: hash node offset
2444 * @algo: double pointer to char, will hold pointer to the algorithm name
2445 *
2446 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2447 * If the property is found its data start address is returned to the caller.
2448 *
2449 * returns:
2450 * 0, on success
2451 * -1, on failure
2452 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002453int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002454{
2455 int len;
2456
Stephen Warren6904b6e2011-10-18 11:11:49 +00002457 *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002458 if (*algo == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002459 fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002460 return -1;
2461 }
2462
2463 return 0;
2464}
2465
2466/**
2467 * fit_image_hash_get_value - get hash value and length
2468 * @fit: pointer to the FIT format image header
2469 * @noffset: hash node offset
2470 * @value: double pointer to uint8_t, will hold address of a hash value data
2471 * @value_len: pointer to an int, will hold hash data length
2472 *
2473 * fit_image_hash_get_value() finds hash value property in a given hash node.
2474 * If the property is found its data start address and size are returned to
2475 * the caller.
2476 *
2477 * returns:
2478 * 0, on success
2479 * -1, on failure
2480 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002481int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002482 int *value_len)
2483{
2484 int len;
2485
Stephen Warren6904b6e2011-10-18 11:11:49 +00002486 *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002487 if (*value == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002488 fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002489 *value_len = 0;
2490 return -1;
2491 }
2492
2493 *value_len = len;
2494 return 0;
2495}
2496
2497/**
2498 * fit_set_timestamp - set node timestamp property
2499 * @fit: pointer to the FIT format image header
2500 * @noffset: node offset
2501 * @timestamp: timestamp value to be set
2502 *
2503 * fit_set_timestamp() attempts to set timestamp property in the requested
2504 * node and returns operation status to the caller.
2505 *
2506 * returns:
2507 * 0, on success
2508 * -1, on property read failure
2509 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002510int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002511{
2512 uint32_t t;
2513 int ret;
2514
Stephen Warren6904b6e2011-10-18 11:11:49 +00002515 t = cpu_to_uimage(timestamp);
2516 ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
2517 sizeof(uint32_t));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002518 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002519 printf("Can't set '%s' property for '%s' node (%s)\n",
2520 FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
2521 fdt_strerror(ret));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002522 return -1;
2523 }
2524
2525 return 0;
2526}
2527
2528/**
2529 * calculate_hash - calculate and return hash for provided input data
2530 * @data: pointer to the input data
2531 * @data_len: data length
2532 * @algo: requested hash algorithm
2533 * @value: pointer to the char, will hold hash value data (caller must
2534 * allocate enough free space)
2535 * value_len: length of the calculated hash
2536 *
2537 * calculate_hash() computes input data hash according to the requested algorithm.
2538 * Resulting hash value is placed in caller provided 'value' buffer, length
2539 * of the calculated hash is returned via value_len pointer argument.
2540 *
2541 * returns:
2542 * 0, on success
2543 * -1, when algo is unsupported
2544 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002545static int calculate_hash(const void *data, int data_len, const char *algo,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002546 uint8_t *value, int *value_len)
2547{
Stephen Warren6904b6e2011-10-18 11:11:49 +00002548 if (strcmp(algo, "crc32") == 0) {
2549 *((uint32_t *)value) = crc32_wd(0, data, data_len,
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002550 CHUNKSZ_CRC32);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002551 *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002552 *value_len = 4;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002553 } else if (strcmp(algo, "sha1") == 0) {
2554 sha1_csum_wd((unsigned char *) data, data_len,
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002555 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002556 *value_len = 20;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002557 } else if (strcmp(algo, "md5") == 0) {
2558 md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01002559 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002560 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002561 debug("Unsupported hash alogrithm\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002562 return -1;
2563 }
2564 return 0;
2565}
2566
2567#ifdef USE_HOSTCC
2568/**
2569 * fit_set_hashes - process FIT component image nodes and calculate hashes
2570 * @fit: pointer to the FIT format image header
2571 *
2572 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2573 * Hashes are calculated for all component images which have hash subnodes
2574 * with algorithm property set to one of the supported hash algorithms.
2575 *
2576 * returns
2577 * 0, on success
2578 * libfdt error code, on failure
2579 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002580int fit_set_hashes(void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002581{
2582 int images_noffset;
2583 int noffset;
2584 int ndepth;
2585 int ret;
2586
2587 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002588 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002589 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002590 printf("Can't find images parent node '%s' (%s)\n",
2591 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002592 return images_noffset;
2593 }
2594
2595 /* Process its subnodes, print out component images details */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002596 for (ndepth = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002597 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002598 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002599 if (ndepth == 1) {
2600 /*
2601 * Direct child node of the images parent node,
2602 * i.e. component image node.
2603 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002604 ret = fit_image_set_hashes(fit, noffset);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002605 if (ret)
2606 return ret;
2607 }
2608 }
2609
2610 return 0;
2611}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01002612
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002613/**
2614 * fit_image_set_hashes - calculate/set hashes for given component image node
2615 * @fit: pointer to the FIT format image header
2616 * @image_noffset: requested component image node
2617 *
2618 * fit_image_set_hashes() adds hash values for an component image node. All
2619 * existing hash subnodes are checked, if algorithm property is set to one of
2620 * the supported hash algorithms, hash value is computed and corresponding
2621 * hash node property is set, for example:
2622 *
2623 * Input component image node structure:
2624 *
2625 * o image@1 (at image_noffset)
2626 * | - data = [binary data]
2627 * o hash@1
2628 * |- algo = "sha1"
2629 *
2630 * Output component image node structure:
2631 *
2632 * o image@1 (at image_noffset)
2633 * | - data = [binary data]
2634 * o hash@1
2635 * |- algo = "sha1"
2636 * |- value = sha1(data)
2637 *
2638 * returns:
2639 * 0 on sucess
2640 * <0 on failure
2641 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002642int fit_image_set_hashes(void *fit, int image_noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002643{
2644 const void *data;
2645 size_t size;
2646 char *algo;
2647 uint8_t value[FIT_MAX_HASH_LEN];
2648 int value_len;
2649 int noffset;
2650 int ndepth;
2651
2652 /* Get image data and data length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002653 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2654 printf("Can't get image data/size\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002655 return -1;
2656 }
2657
2658 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002659 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002660 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002661 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002662 if (ndepth == 1) {
2663 /* Direct child node of the component image node */
2664
2665 /*
2666 * Check subnode name, must be equal to "hash".
2667 * Multiple hash nodes require unique unit node
2668 * names, e.g. hash@1, hash@2, etc.
2669 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002670 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002671 FIT_HASH_NODENAME,
2672 strlen(FIT_HASH_NODENAME)) != 0) {
2673 /* Not a hash subnode, skip it */
2674 continue;
2675 }
2676
Stephen Warren6904b6e2011-10-18 11:11:49 +00002677 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
2678 printf("Can't get hash algo property for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002679 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002680 fit_get_name(fit, noffset, NULL),
2681 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002682 return -1;
2683 }
2684
Stephen Warren6904b6e2011-10-18 11:11:49 +00002685 if (calculate_hash(data, size, algo, value,
2686 &value_len)) {
2687 printf("Unsupported hash algorithm (%s) for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002688 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002689 algo, fit_get_name(fit, noffset, NULL),
2690 fit_get_name(fit, image_noffset,
2691 NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002692 return -1;
2693 }
2694
Stephen Warren6904b6e2011-10-18 11:11:49 +00002695 if (fit_image_hash_set_value(fit, noffset, value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002696 value_len)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002697 printf("Can't set hash value for "
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002698 "'%s' hash node in '%s' image node\n",
Stephen Warren6904b6e2011-10-18 11:11:49 +00002699 fit_get_name(fit, noffset, NULL),
2700 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002701 return -1;
2702 }
2703 }
2704 }
2705
2706 return 0;
2707}
2708
2709/**
2710 * fit_image_hash_set_value - set hash value in requested has node
2711 * @fit: pointer to the FIT format image header
2712 * @noffset: hash node offset
2713 * @value: hash value to be set
2714 * @value_len: hash value length
2715 *
2716 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2717 * given and returns operation status to the caller.
2718 *
2719 * returns
2720 * 0, on success
2721 * -1, on failure
2722 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002723int fit_image_hash_set_value(void *fit, int noffset, uint8_t *value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002724 int value_len)
2725{
2726 int ret;
2727
Stephen Warren6904b6e2011-10-18 11:11:49 +00002728 ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002729 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002730 printf("Can't set hash '%s' property for '%s' node(%s)\n",
2731 FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
2732 fdt_strerror(ret));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002733 return -1;
2734 }
2735
2736 return 0;
2737}
2738#endif /* USE_HOSTCC */
2739
2740/**
2741 * fit_image_check_hashes - verify data intergity
2742 * @fit: pointer to the FIT format image header
2743 * @image_noffset: component image node offset
2744 *
2745 * fit_image_check_hashes() goes over component image hash nodes,
2746 * re-calculates each data hash and compares with the value stored in hash
2747 * node.
2748 *
2749 * returns:
2750 * 1, if all hashes are valid
2751 * 0, otherwise (or on error)
2752 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002753int fit_image_check_hashes(const void *fit, int image_noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002754{
2755 const void *data;
2756 size_t size;
2757 char *algo;
2758 uint8_t *fit_value;
2759 int fit_value_len;
2760 uint8_t value[FIT_MAX_HASH_LEN];
2761 int value_len;
2762 int noffset;
2763 int ndepth;
2764 char *err_msg = "";
2765
2766 /* Get image data and data length */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002767 if (fit_image_get_data(fit, image_noffset, &data, &size)) {
2768 printf("Can't get image data/size\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002769 return 0;
2770 }
2771
2772 /* Process all hash subnodes of the component image node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002773 for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002774 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002775 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002776 if (ndepth == 1) {
2777 /* Direct child node of the component image node */
2778
2779 /*
2780 * Check subnode name, must be equal to "hash".
2781 * Multiple hash nodes require unique unit node
2782 * names, e.g. hash@1, hash@2, etc.
2783 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002784 if (strncmp(fit_get_name(fit, noffset, NULL),
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002785 FIT_HASH_NODENAME,
2786 strlen(FIT_HASH_NODENAME)) != 0)
2787 continue;
2788
Stephen Warren6904b6e2011-10-18 11:11:49 +00002789 if (fit_image_hash_get_algo(fit, noffset, &algo)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002790 err_msg = " error!\nCan't get hash algo "
2791 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002792 goto error;
2793 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002794 printf("%s", algo);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002795
Stephen Warren6904b6e2011-10-18 11:11:49 +00002796 if (fit_image_hash_get_value(fit, noffset, &fit_value,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002797 &fit_value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002798 err_msg = " error!\nCan't get hash value "
2799 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002800 goto error;
2801 }
2802
Stephen Warren6904b6e2011-10-18 11:11:49 +00002803 if (calculate_hash(data, size, algo, value,
2804 &value_len)) {
2805 err_msg = " error!\n"
2806 "Unsupported hash algorithm";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002807 goto error;
2808 }
2809
2810 if (value_len != fit_value_len) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002811 err_msg = " error !\nBad hash value len";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002812 goto error;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002813 } else if (memcmp(value, fit_value, value_len) != 0) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002814 err_msg = " error!\nBad hash value";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002815 goto error;
2816 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00002817 printf("+ ");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002818 }
2819 }
2820
2821 return 1;
2822
2823error:
Stephen Warren6904b6e2011-10-18 11:11:49 +00002824 printf("%s for '%s' hash node in '%s' image node\n",
2825 err_msg, fit_get_name(fit, noffset, NULL),
2826 fit_get_name(fit, image_noffset, NULL));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002827 return 0;
2828}
2829
2830/**
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002831 * fit_all_image_check_hashes - verify data intergity for all images
2832 * @fit: pointer to the FIT format image header
2833 *
2834 * fit_all_image_check_hashes() goes over all images in the FIT and
2835 * for every images checks if all it's hashes are valid.
2836 *
2837 * returns:
2838 * 1, if all hashes of all images are valid
2839 * 0, otherwise (or on error)
2840 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002841int fit_all_image_check_hashes(const void *fit)
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002842{
2843 int images_noffset;
2844 int noffset;
2845 int ndepth;
2846 int count;
2847
2848 /* Find images parent node offset */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002849 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002850 if (images_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00002851 printf("Can't find images parent node '%s' (%s)\n",
2852 FIT_IMAGES_PATH, fdt_strerror(images_noffset));
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002853 return 0;
2854 }
2855
2856 /* Process all image subnodes, check hashes for each */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002857 printf("## Checking hash(es) for FIT Image at %08lx ...\n",
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002858 (ulong)fit);
2859 for (ndepth = 0, count = 0,
Stephen Warren6904b6e2011-10-18 11:11:49 +00002860 noffset = fdt_next_node(fit, images_noffset, &ndepth);
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002861 (noffset >= 0) && (ndepth > 0);
Stephen Warren6904b6e2011-10-18 11:11:49 +00002862 noffset = fdt_next_node(fit, noffset, &ndepth)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002863 if (ndepth == 1) {
2864 /*
2865 * Direct child node of the images parent node,
2866 * i.e. component image node.
2867 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002868 printf(" Hash(es) for Image %u (%s): ", count++,
2869 fit_get_name(fit, noffset, NULL));
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002870
Stephen Warren6904b6e2011-10-18 11:11:49 +00002871 if (!fit_image_check_hashes(fit, noffset))
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002872 return 0;
Stephen Warren6904b6e2011-10-18 11:11:49 +00002873 printf("\n");
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002874 }
2875 }
2876 return 1;
2877}
2878
2879/**
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002880 * fit_image_check_os - check whether image node is of a given os type
2881 * @fit: pointer to the FIT format image header
2882 * @noffset: component image node offset
2883 * @os: requested image os
2884 *
2885 * fit_image_check_os() reads image os property and compares its numeric
2886 * id with the requested os. Comparison result is returned to the caller.
2887 *
2888 * returns:
2889 * 1 if image is of given os type
2890 * 0 otherwise (or on error)
2891 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002892int fit_image_check_os(const void *fit, int noffset, uint8_t os)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002893{
2894 uint8_t image_os;
2895
Stephen Warren6904b6e2011-10-18 11:11:49 +00002896 if (fit_image_get_os(fit, noffset, &image_os))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002897 return 0;
2898 return (os == image_os);
2899}
2900
2901/**
2902 * fit_image_check_arch - check whether image node is of a given arch
2903 * @fit: pointer to the FIT format image header
2904 * @noffset: component image node offset
2905 * @arch: requested imagearch
2906 *
2907 * fit_image_check_arch() reads image arch property and compares its numeric
2908 * id with the requested arch. Comparison result is returned to the caller.
2909 *
2910 * returns:
2911 * 1 if image is of given arch
2912 * 0 otherwise (or on error)
2913 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002914int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002915{
2916 uint8_t image_arch;
2917
Stephen Warren6904b6e2011-10-18 11:11:49 +00002918 if (fit_image_get_arch(fit, noffset, &image_arch))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002919 return 0;
2920 return (arch == image_arch);
2921}
2922
2923/**
2924 * fit_image_check_type - check whether image node is of a given type
2925 * @fit: pointer to the FIT format image header
2926 * @noffset: component image node offset
2927 * @type: requested image type
2928 *
2929 * fit_image_check_type() reads image type property and compares its numeric
2930 * id with the requested type. Comparison result is returned to the caller.
2931 *
2932 * returns:
2933 * 1 if image is of given type
2934 * 0 otherwise (or on error)
2935 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002936int fit_image_check_type(const void *fit, int noffset, uint8_t type)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002937{
2938 uint8_t image_type;
2939
Stephen Warren6904b6e2011-10-18 11:11:49 +00002940 if (fit_image_get_type(fit, noffset, &image_type))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002941 return 0;
2942 return (type == image_type);
2943}
2944
2945/**
2946 * fit_image_check_comp - check whether image node uses given compression
2947 * @fit: pointer to the FIT format image header
2948 * @noffset: component image node offset
2949 * @comp: requested image compression type
2950 *
2951 * fit_image_check_comp() reads image compression property and compares its
2952 * numeric id with the requested compression type. Comparison result is
2953 * returned to the caller.
2954 *
2955 * returns:
2956 * 1 if image uses requested compression
2957 * 0 otherwise (or on error)
2958 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002959int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002960{
2961 uint8_t image_comp;
2962
Stephen Warren6904b6e2011-10-18 11:11:49 +00002963 if (fit_image_get_comp(fit, noffset, &image_comp))
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002964 return 0;
2965 return (comp == image_comp);
2966}
2967
2968/**
2969 * fit_check_format - sanity check FIT image format
2970 * @fit: pointer to the FIT format image header
2971 *
2972 * fit_check_format() runs a basic sanity FIT image verification.
2973 * Routine checks for mandatory properties, nodes, etc.
2974 *
2975 * returns:
2976 * 1, on success
2977 * 0, on failure
2978 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002979int fit_check_format(const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002980{
2981 /* mandatory / node 'description' property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002982 if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2983 debug("Wrong FIT format: no description\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002984 return 0;
2985 }
2986
2987#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2988 /* mandatory / node 'timestamp' property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002989 if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2990 debug("Wrong FIT format: no timestamp\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002991 return 0;
2992 }
2993#endif
2994
2995 /* mandatory subimages parent '/images' node */
Stephen Warren6904b6e2011-10-18 11:11:49 +00002996 if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
2997 debug("Wrong FIT format: no images parent node\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002998 return 0;
2999 }
3000
3001 return 1;
3002}
3003
3004/**
3005 * fit_conf_get_node - get node offset for configuration of a given unit name
3006 * @fit: pointer to the FIT format image header
3007 * @conf_uname: configuration node unit name
3008 *
3009 * fit_conf_get_node() finds a configuration (withing the '/configurations'
3010 * parant node) of a provided unit name. If configuration is found its node offset
3011 * is returned to the caller.
3012 *
3013 * When NULL is provided in second argument fit_conf_get_node() will search
3014 * for a default configuration node instead. Default configuration node unit name
3015 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
3016 *
3017 * returns:
3018 * configuration node offset when found (>=0)
3019 * negative number on failure (FDT_ERR_* code)
3020 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003021int fit_conf_get_node(const void *fit, const char *conf_uname)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003022{
3023 int noffset, confs_noffset;
3024 int len;
3025
Stephen Warren6904b6e2011-10-18 11:11:49 +00003026 confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003027 if (confs_noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003028 debug("Can't find configurations parent node '%s' (%s)\n",
3029 FIT_CONFS_PATH, fdt_strerror(confs_noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003030 return confs_noffset;
3031 }
3032
3033 if (conf_uname == NULL) {
3034 /* get configuration unit name from the default property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003035 debug("No configuration specified, trying default...\n");
3036 conf_uname = (char *)fdt_getprop(fit, confs_noffset,
3037 FIT_DEFAULT_PROP, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003038 if (conf_uname == NULL) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003039 fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
3040 len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003041 return len;
3042 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00003043 debug("Found default configuration: '%s'\n", conf_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003044 }
3045
Stephen Warren6904b6e2011-10-18 11:11:49 +00003046 noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003047 if (noffset < 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003048 debug("Can't get node offset for configuration unit name: "
3049 "'%s' (%s)\n",
3050 conf_uname, fdt_strerror(noffset));
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003051 }
3052
3053 return noffset;
3054}
3055
Stephen Warren6904b6e2011-10-18 11:11:49 +00003056static int __fit_conf_get_prop_node(const void *fit, int noffset,
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003057 const char *prop_name)
3058{
3059 char *uname;
3060 int len;
3061
3062 /* get kernel image unit name from configuration kernel property */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003063 uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003064 if (uname == NULL)
3065 return len;
3066
Stephen Warren6904b6e2011-10-18 11:11:49 +00003067 return fit_image_get_node(fit, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003068}
3069
3070/**
3071 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
3072 * a given configuration
3073 * @fit: pointer to the FIT format image header
3074 * @noffset: configuration node offset
3075 *
3076 * fit_conf_get_kernel_node() retrives kernel image node unit name from
3077 * configuration FIT_KERNEL_PROP property and translates it to the node
3078 * offset.
3079 *
3080 * returns:
3081 * image node offset when found (>=0)
3082 * negative number on failure (FDT_ERR_* code)
3083 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003084int fit_conf_get_kernel_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003085{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003086 return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003087}
3088
3089/**
3090 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
3091 * a given configuration
3092 * @fit: pointer to the FIT format image header
3093 * @noffset: configuration node offset
3094 *
3095 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
3096 * configuration FIT_KERNEL_PROP property and translates it to the node
3097 * offset.
3098 *
3099 * returns:
3100 * image node offset when found (>=0)
3101 * negative number on failure (FDT_ERR_* code)
3102 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003103int fit_conf_get_ramdisk_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003104{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003105 return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003106}
3107
3108/**
3109 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
3110 * a given configuration
3111 * @fit: pointer to the FIT format image header
3112 * @noffset: configuration node offset
3113 *
3114 * fit_conf_get_fdt_node() retrives fdt image node unit name from
3115 * configuration FIT_KERNEL_PROP property and translates it to the node
3116 * offset.
3117 *
3118 * returns:
3119 * image node offset when found (>=0)
3120 * negative number on failure (FDT_ERR_* code)
3121 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003122int fit_conf_get_fdt_node(const void *fit, int noffset)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003123{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003124 return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003125}
3126
3127/**
3128 * fit_conf_print - prints out the FIT configuration details
3129 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01003130 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003131 * @p: pointer to prefix string
3132 *
3133 * fit_conf_print() lists all mandatory properies for the processed
3134 * configuration node.
3135 *
3136 * returns:
3137 * no returned results
3138 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003139void fit_conf_print(const void *fit, int noffset, const char *p)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003140{
3141 char *desc;
3142 char *uname;
3143 int ret;
3144
3145 /* Mandatory properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003146 ret = fit_get_desc(fit, noffset, &desc);
3147 printf("%s Description: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003148 if (ret)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003149 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003150 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00003151 printf("%s\n", desc);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003152
Stephen Warren6904b6e2011-10-18 11:11:49 +00003153 uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
3154 printf("%s Kernel: ", p);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003155 if (uname == NULL)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003156 printf("unavailable\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003157 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00003158 printf("%s\n", uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003159
3160 /* Optional properties */
Stephen Warren6904b6e2011-10-18 11:11:49 +00003161 uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003162 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003163 printf("%s Init Ramdisk: %s\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003164
Stephen Warren6904b6e2011-10-18 11:11:49 +00003165 uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003166 if (uname)
Stephen Warren6904b6e2011-10-18 11:11:49 +00003167 printf("%s FDT: %s\n", p, uname);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01003168}
Marian Balakowicza50d5152008-03-12 10:12:37 +01003169
3170/**
3171 * fit_check_ramdisk - verify FIT format ramdisk subimage
3172 * @fit_hdr: pointer to the FIT ramdisk header
3173 * @rd_noffset: ramdisk subimage node offset within FIT image
3174 * @arch: requested ramdisk image architecture type
3175 * @verify: data CRC verification flag
3176 *
3177 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3178 * specified FIT image.
3179 *
3180 * returns:
3181 * 1, on success
3182 * 0, on failure
3183 */
3184#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +00003185static int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
3186 int verify)
Marian Balakowicza50d5152008-03-12 10:12:37 +01003187{
Stephen Warren6904b6e2011-10-18 11:11:49 +00003188 fit_image_print(fit, rd_noffset, " ");
Marian Balakowicza50d5152008-03-12 10:12:37 +01003189
3190 if (verify) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00003191 puts(" Verifying Hash Integrity ... ");
3192 if (!fit_image_check_hashes(fit, rd_noffset)) {
3193 puts("Bad Data Hash\n");
Simon Glass0169e6b2012-02-13 13:51:18 +00003194 bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003195 return 0;
3196 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00003197 puts("OK\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +01003198 }
3199
Simon Glass0169e6b2012-02-13 13:51:18 +00003200 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
Stephen Warren6904b6e2011-10-18 11:11:49 +00003201 if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
3202 !fit_image_check_arch(fit, rd_noffset, arch) ||
3203 !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
3204 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicza50d5152008-03-12 10:12:37 +01003205 genimg_get_arch_name(arch));
Simon Glass0169e6b2012-02-13 13:51:18 +00003206 bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003207 return 0;
3208 }
3209
Simon Glass0169e6b2012-02-13 13:51:18 +00003210 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003211 return 1;
3212}
3213#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01003214#endif /* CONFIG_FIT */