blob: 6d8833e814d841a46d7124514063ce8767faf3d8 [file] [log] [blame]
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010025
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010027#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
Marian Balakowicz95d449a2008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicz2242f532008-02-21 17:27:41 +010042#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010046#include <image.h>
47
Marian Balakowiczc8779642008-03-12 10:12:37 +010048#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +010049#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010052#endif
53
54#if defined(CONFIG_FIT)
Andy Fleming20a14a42008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010056#include <sha1.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010057
58static int fit_check_ramdisk (const void *fit, int os_noffset,
59 uint8_t arch, int verify);
Marian Balakowiczfff888a12008-02-21 17:20:19 +010060#endif
61
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010062#ifdef CONFIG_CMD_BDI
Wolfgang Denk54841ab2010-06-28 22:00:46 +020063extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010064#endif
65
66DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010067
Wolfgang Denk3a2003f2009-08-19 11:42:56 +020068static const image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010069 int verify);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010070#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010076
Marian Balakowicz570abb02008-02-29 15:59:59 +010077static table_entry_t uimage_arch[] = {
78 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
79 { IH_ARCH_ALPHA, "alpha", "Alpha", },
80 { IH_ARCH_ARM, "arm", "ARM", },
81 { IH_ARCH_I386, "x86", "Intel x86", },
82 { IH_ARCH_IA64, "ia64", "IA64", },
83 { IH_ARCH_M68K, "m68k", "M68K", },
84 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
85 { IH_ARCH_MIPS, "mips", "MIPS", },
86 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010087 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070088 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010089 { IH_ARCH_PPC, "ppc", "PowerPC", },
90 { IH_ARCH_S390, "s390", "IBM S390", },
91 { IH_ARCH_SH, "sh", "SuperH", },
92 { IH_ARCH_SPARC, "sparc", "SPARC", },
93 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
94 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
95 { IH_ARCH_AVR32, "avr32", "AVR32", },
96 { -1, "", "", },
97};
98
99static table_entry_t uimage_os[] = {
100 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100101 { IH_OS_LINUX, "linux", "Linux", },
102#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
103 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
104#endif
105 { IH_OS_NETBSD, "netbsd", "NetBSD", },
106 { IH_OS_RTEMS, "rtems", "RTEMS", },
107 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
108#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
109 { IH_OS_QNX, "qnx", "QNX", },
110 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
111#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500112#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
113 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
114#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100115#ifdef USE_HOSTCC
116 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
117 { IH_OS_DELL, "dell", "Dell", },
118 { IH_OS_ESIX, "esix", "Esix", },
119 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
120 { IH_OS_IRIX, "irix", "Irix", },
121 { IH_OS_NCR, "ncr", "NCR", },
122 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
123 { IH_OS_PSOS, "psos", "pSOS", },
124 { IH_OS_SCO, "sco", "SCO", },
125 { IH_OS_SOLARIS, "solaris", "Solaris", },
126 { IH_OS_SVR4, "svr4", "SVR4", },
127#endif
128 { -1, "", "", },
129};
130
131static table_entry_t uimage_type[] = {
132 { IH_TYPE_INVALID, NULL, "Invalid Image", },
133 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
134 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
135 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
136 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
137 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
138 { IH_TYPE_SCRIPT, "script", "Script", },
139 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
140 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Prafulla Wadaskaraa0c7a82009-09-07 15:05:02 +0530141 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
Stefano Babic8edcde52010-01-20 18:19:10 +0100142 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100143 { -1, "", "", },
144};
145
146static table_entry_t uimage_comp[] = {
147 { IH_COMP_NONE, "none", "uncompressed", },
148 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
149 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200150 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100151 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100152 { -1, "", "", },
153};
154
Mike Frysinger89cdab72008-03-31 11:02:01 -0400155uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200156uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100157#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
158static void genimg_print_time (time_t timestamp);
159#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100160
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100161/*****************************************************************************/
162/* Legacy format routines */
163/*****************************************************************************/
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200164int image_check_hcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100165{
166 ulong hcrc;
167 ulong len = image_get_header_size ();
168 image_header_t header;
169
170 /* Copy header so we can blank CRC field for re-calculation */
171 memmove (&header, (char *)hdr, image_get_header_size ());
172 image_set_hcrc (&header, 0);
173
174 hcrc = crc32 (0, (unsigned char *)&header, len);
175
176 return (hcrc == image_get_hcrc (hdr));
177}
178
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200179int image_check_dcrc (const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100180{
181 ulong data = image_get_data (hdr);
182 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200183 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100184
185 return (dcrc == image_get_dcrc (hdr));
186}
187
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100188/**
189 * image_multi_count - get component (sub-image) count
190 * @hdr: pointer to the header of the multi component image
191 *
192 * image_multi_count() returns number of components in a multi
193 * component image.
194 *
195 * Note: no checking of the image type is done, caller must pass
196 * a valid multi component image.
197 *
198 * returns:
199 * number of components
200 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200201ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100202{
203 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100204 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100205
206 /* get start of the image payload, which in case of multi
207 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100208 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100209
210 /* count non empty slots */
211 for (i = 0; size[i]; ++i)
212 count++;
213
214 return count;
215}
216
217/**
218 * image_multi_getimg - get component data address and size
219 * @hdr: pointer to the header of the multi component image
220 * @idx: index of the requested component
221 * @data: pointer to a ulong variable, will hold component data address
222 * @len: pointer to a ulong variable, will hold component size
223 *
224 * image_multi_getimg() returns size and data address for the requested
225 * component in a multi component image.
226 *
227 * Note: no checking of the image type is done, caller must pass
228 * a valid multi component image.
229 *
230 * returns:
231 * data address and size of the component, if idx is valid
232 * 0 in data and len, if idx is out of range
233 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200234void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100235 ulong *data, ulong *len)
236{
237 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100238 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700239 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100240
241 /* get number of component */
242 count = image_multi_count (hdr);
243
244 /* get start of the image payload, which in case of multi
245 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100246 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100247
248 /* get address of the proper component data start, which means
249 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100250 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100251
252 if (idx < count) {
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100253 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100254 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100255
256 /* go over all indices preceding requested component idx */
257 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700258 /* add up i-th component size, rounding up to 4 bytes */
259 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100260 }
261
262 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700263 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100264 } else {
265 *len = 0;
266 *data = 0;
267 }
268}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100269
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200270static void image_print_type (const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100271{
272 const char *os, *arch, *type, *comp;
273
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100274 os = genimg_get_os_name (image_get_os (hdr));
275 arch = genimg_get_arch_name (image_get_arch (hdr));
276 type = genimg_get_type_name (image_get_type (hdr));
277 comp = genimg_get_comp_name (image_get_comp (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100278
Marian Balakowicz570abb02008-02-29 15:59:59 +0100279 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100280}
281
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100282/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200283 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200284 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100285 * @p: pointer to prefix string
286 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200287 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100288 * The routine prints out all header fields followed by the size/offset data
289 * for MULTI/SCRIPT images.
290 *
291 * returns:
292 * no returned results
293 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200294void image_print_contents (const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100295{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200296 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200297 const char *p;
298
299#ifdef USE_HOSTCC
300 p = "";
301#else
302 p = " ";
303#endif
304
Marian Balakowicz570abb02008-02-29 15:59:59 +0100305 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
306#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
307 printf ("%sCreated: ", p);
308 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100309#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100310 printf ("%sImage Type: ", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100311 image_print_type (hdr);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100312 printf ("%sData Size: ", p);
313 genimg_print_size (image_get_data_size (hdr));
314 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
315 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100316
Marian Balakowicz570abb02008-02-29 15:59:59 +0100317 if (image_check_type (hdr, IH_TYPE_MULTI) ||
318 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100319 int i;
320 ulong data, len;
321 ulong count = image_multi_count (hdr);
322
Marian Balakowicz570abb02008-02-29 15:59:59 +0100323 printf ("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100324 for (i = 0; i < count; i++) {
325 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100326
327 printf ("%s Image %d: ", p, i);
328 genimg_print_size (len);
329
330 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
331 /*
332 * the user may need to know offsets
333 * if planning to do something with
334 * multiple files
335 */
336 printf ("%s Offset = 0x%08lx\n", p, data);
337 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100338 }
339 }
340}
341
Marian Balakowicz570abb02008-02-29 15:59:59 +0100342
343#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100344/**
345 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100346 * @rd_addr: ramdisk image start address
347 * @arch: expected ramdisk architecture
348 * @verify: checksum verification flag
349 *
350 * image_get_ramdisk() returns a pointer to the verified ramdisk image
351 * header. Routine receives image start address and expected architecture
352 * flag. Verification done covers data and header integrity and os/type/arch
353 * fields checking.
354 *
355 * If dataflash support is enabled routine checks for dataflash addresses
356 * and handles required dataflash reads.
357 *
358 * returns:
359 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600360 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100361 */
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200362static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100363 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100364{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200365 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100366
367 if (!image_check_magic (rd_hdr)) {
368 puts ("Bad Magic Number\n");
369 show_boot_progress (-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600370 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100371 }
372
373 if (!image_check_hcrc (rd_hdr)) {
374 puts ("Bad Header Checksum\n");
375 show_boot_progress (-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600376 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100377 }
378
379 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100380 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100381
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100382 if (verify) {
383 puts(" Verifying Checksum ... ");
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200384 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100385 puts ("Bad Data CRC\n");
386 show_boot_progress (-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600387 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100388 }
389 puts("OK\n");
390 }
391
392 show_boot_progress (11);
393
394 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
395 !image_check_arch (rd_hdr, arch) ||
396 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
397 printf ("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100398 genimg_get_arch_name(arch));
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100399 show_boot_progress (-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600400 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100401 }
402
403 return rd_hdr;
404}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100405#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100406
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100407/*****************************************************************************/
408/* Shared dual-format routines */
409/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100410#ifndef USE_HOSTCC
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200411int getenv_yesno (char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100412{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200413 char *s = getenv (var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100414 return (s && (*s == 'n')) ? 0 : 1;
415}
416
417ulong getenv_bootm_low(void)
418{
419 char *s = getenv ("bootm_low");
420 if (s) {
421 ulong tmp = simple_strtoul (s, NULL, 16);
422 return tmp;
423 }
424
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200425#if defined(CONFIG_SYS_SDRAM_BASE)
426 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100427#elif defined(CONFIG_ARM)
428 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100429#else
430 return 0;
431#endif
432}
433
Becky Bruce391fd932008-06-09 20:37:18 -0500434phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100435{
436 char *s = getenv ("bootm_size");
437 if (s) {
Becky Bruce391fd932008-06-09 20:37:18 -0500438 phys_size_t tmp;
Becky Bruce391fd932008-06-09 20:37:18 -0500439 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100440 return tmp;
441 }
442
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100443#if defined(CONFIG_ARM)
444 return gd->bd->bi_dram[0].size;
445#else
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100446 return gd->bd->bi_memsize;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100447#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100448}
449
450void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
451{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400452 if (to == from)
453 return;
454
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100455#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
456 while (len > 0) {
457 size_t tail = (len > chunksz) ? chunksz : len;
458 WATCHDOG_RESET ();
459 memmove (to, from, tail);
460 to += tail;
461 from += tail;
462 len -= tail;
463 }
464#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
465 memmove (to, from, len);
466#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
467}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100468#endif /* !USE_HOSTCC */
469
Prafulla Wadaskarf666dea2009-09-07 14:59:07 +0530470void genimg_print_size (uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100471{
472#ifndef USE_HOSTCC
473 printf ("%d Bytes = ", size);
474 print_size (size, "\n");
475#else
476 printf ("%d Bytes = %.2f kB = %.2f MB\n",
477 size, (double)size / 1.024e3,
478 (double)size / 1.048576e6);
479#endif
480}
481
482#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
483static void genimg_print_time (time_t timestamp)
484{
485#ifndef USE_HOSTCC
486 struct rtc_time tm;
487
488 to_tm (timestamp, &tm);
489 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
490 tm.tm_year, tm.tm_mon, tm.tm_mday,
491 tm.tm_hour, tm.tm_min, tm.tm_sec);
492#else
493 printf ("%s", ctime(&timestamp));
494#endif
495}
496#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
497
498/**
499 * get_table_entry_name - translate entry id to long name
500 * @table: pointer to a translation table for entries of a specific type
501 * @msg: message to be returned when translation fails
502 * @id: entry id to be translated
503 *
504 * get_table_entry_name() will go over translation table trying to find
505 * entry that matches given id. If matching entry is found, its long
506 * name is returned to the caller.
507 *
508 * returns:
509 * long entry name if translation succeeds
510 * msg otherwise
511 */
Prafulla Wadaskarb029ddd2009-09-07 14:59:08 +0530512char *get_table_entry_name (table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100513{
514 for (; table->id >= 0; ++table) {
515 if (table->id == id)
Peter Tyser521af042009-09-21 11:20:36 -0500516#if defined(USE_HOSTCC) || defined(CONFIG_RELOC_FIXUP_WORKS)
Scott Woode3d1ac72009-04-02 16:15:10 -0500517 return table->lname;
518#else
519 return table->lname + gd->reloc_off;
520#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100521 }
522 return (msg);
523}
524
525const char *genimg_get_os_name (uint8_t os)
526{
527 return (get_table_entry_name (uimage_os, "Unknown OS", os));
528}
529
530const char *genimg_get_arch_name (uint8_t arch)
531{
532 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
533}
534
535const char *genimg_get_type_name (uint8_t type)
536{
537 return (get_table_entry_name (uimage_type, "Unknown Image", type));
538}
539
540const char *genimg_get_comp_name (uint8_t comp)
541{
542 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
543}
544
545/**
546 * get_table_entry_id - translate short entry name to id
547 * @table: pointer to a translation table for entries of a specific type
548 * @table_name: to be used in case of error
549 * @name: entry short name to be translated
550 *
551 * get_table_entry_id() will go over translation table trying to find
552 * entry that matches given short name. If matching entry is found,
553 * its id returned to the caller.
554 *
555 * returns:
556 * entry id if translation succeeds
557 * -1 otherwise
558 */
Prafulla Wadaskarb029ddd2009-09-07 14:59:08 +0530559int get_table_entry_id (table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100560 const char *table_name, const char *name)
561{
562 table_entry_t *t;
563#ifdef USE_HOSTCC
564 int first = 1;
565
566 for (t = table; t->id >= 0; ++t) {
567 if (t->sname && strcasecmp(t->sname, name) == 0)
568 return (t->id);
569 }
570
571 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
572 for (t = table; t->id >= 0; ++t) {
573 if (t->sname == NULL)
574 continue;
575 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
576 first = 0;
577 }
578 fprintf (stderr, "\n");
579#else
580 for (t = table; t->id >= 0; ++t) {
Peter Tyser521af042009-09-21 11:20:36 -0500581#ifdef CONFIG_RELOC_FIXUP_WORKS
582 if (t->sname && strcmp(t->sname, name) == 0)
583#else
Scott Woode3d1ac72009-04-02 16:15:10 -0500584 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500585#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100586 return (t->id);
587 }
588 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100589#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100590 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100591}
592
Marian Balakowicz570abb02008-02-29 15:59:59 +0100593int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100594{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100595 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100596}
597
Marian Balakowicz570abb02008-02-29 15:59:59 +0100598int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100599{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100600 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100601}
602
Marian Balakowicz570abb02008-02-29 15:59:59 +0100603int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100604{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100605 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100606}
607
Marian Balakowicz570abb02008-02-29 15:59:59 +0100608int genimg_get_comp_id (const char *name)
609{
610 return (get_table_entry_id (uimage_comp, "Compression", name));
611}
612
613#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100614/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100615 * genimg_get_format - get image format type
616 * @img_addr: image start address
617 *
618 * genimg_get_format() checks whether provided address points to a valid
619 * legacy or FIT image.
620 *
621 * New uImage format and FDT blob are based on a libfdt. FDT blob
622 * may be passed directly or embedded in a FIT image. In both situations
623 * genimg_get_format() must be able to dectect libfdt header.
624 *
625 * returns:
626 * image format type or IMAGE_FORMAT_INVALID if no image is present
627 */
628int genimg_get_format (void *img_addr)
629{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200630 ulong format = IMAGE_FORMAT_INVALID;
631 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100632#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200633 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100634#endif
635
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200636 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100637 if (image_check_magic(hdr))
638 format = IMAGE_FORMAT_LEGACY;
639#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
640 else {
641 fit_hdr = (char *)img_addr;
642 if (fdt_check_header (fit_hdr) == 0)
643 format = IMAGE_FORMAT_FIT;
644 }
645#endif
646
647 return format;
648}
649
650/**
651 * genimg_get_image - get image from special storage (if necessary)
652 * @img_addr: image start address
653 *
654 * genimg_get_image() checks if provided image start adddress is located
655 * in a dataflash storage. If so, image is moved to a system RAM memory.
656 *
657 * returns:
658 * image start address after possible relocation from special storage
659 */
660ulong genimg_get_image (ulong img_addr)
661{
662 ulong ram_addr = img_addr;
663
664#ifdef CONFIG_HAS_DATAFLASH
665 ulong h_size, d_size;
666
667 if (addr_dataflash (img_addr)){
668 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200669 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100670
671 /* get header size */
672 h_size = image_get_header_size ();
673#if defined(CONFIG_FIT)
674 if (sizeof(struct fdt_header) > h_size)
675 h_size = sizeof(struct fdt_header);
676#endif
677
678 /* read in header */
679 debug (" Reading image header from dataflash address "
680 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
681
682 read_dataflash (img_addr, h_size, (char *)ram_addr);
683
684 /* get data size */
685 switch (genimg_get_format ((void *)ram_addr)) {
686 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200687 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100688 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
689 ram_addr, d_size);
690 break;
691#if defined(CONFIG_FIT)
692 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100693 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100694 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
695 ram_addr, d_size);
696 break;
697#endif
698 default:
699 printf (" No valid image found at 0x%08lx\n", img_addr);
700 return ram_addr;
701 }
702
703 /* read in image data */
704 debug (" Reading image remaining data from dataflash address "
705 "%08lx to RAM address %08lx\n", img_addr + h_size,
706 ram_addr + h_size);
707
708 read_dataflash (img_addr + h_size, d_size,
709 (char *)(ram_addr + h_size));
710
711 }
712#endif /* CONFIG_HAS_DATAFLASH */
713
714 return ram_addr;
715}
716
717/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100718 * fit_has_config - check if there is a valid FIT configuration
719 * @images: pointer to the bootm command headers structure
720 *
721 * fit_has_config() checks if there is a FIT configuration in use
722 * (if FTI support is present).
723 *
724 * returns:
725 * 0, no FIT support or no configuration found
726 * 1, configuration found
727 */
728int genimg_has_config (bootm_headers_t *images)
729{
730#if defined(CONFIG_FIT)
731 if (images->fit_uname_cfg)
732 return 1;
733#endif
734 return 0;
735}
736
737/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100738 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100739 * @argc: command argument count
740 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100741 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100742 * @arch: expected ramdisk architecture
743 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
744 * @rd_end: pointer to a ulong variable, will hold ramdisk end
745 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100746 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100747 * Curently supported are the following ramdisk sources:
748 * - multicomponent kernel/ramdisk image,
749 * - commandline provided address of decicated ramdisk image.
750 *
751 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100752 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100753 * rd_start and rd_end are set to ramdisk start/end addresses if
754 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100755 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500756 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100757 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100758 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200759int boot_get_ramdisk (int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100760 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100761{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100762 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100763 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200764 const image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100765#if defined(CONFIG_FIT)
766 void *fit_hdr;
767 const char *fit_uname_config = NULL;
768 const char *fit_uname_ramdisk = NULL;
769 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100770 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100771 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100772 const void *data;
773 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100774#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100775
Marian Balakowiczc8779642008-03-12 10:12:37 +0100776 *rd_start = 0;
777 *rd_end = 0;
778
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100779 /*
780 * Look for a '-' which indicates to ignore the
781 * ramdisk argument
782 */
783 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
784 debug ("## Skipping init Ramdisk\n");
785 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100786 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100787#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100788 if (argc >= 3) {
789 /*
790 * If the init ramdisk comes from the FIT image and
791 * the FIT image address is omitted in the command
792 * line argument, try to use os FIT image address or
793 * default load address.
794 */
795 if (images->fit_uname_os)
796 default_addr = (ulong)images->fit_hdr_os;
797 else
798 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100799
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100800 if (fit_parse_conf (argv[2], default_addr,
801 &rd_addr, &fit_uname_config)) {
802 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
803 fit_uname_config, rd_addr);
804 } else if (fit_parse_subimage (argv[2], default_addr,
805 &rd_addr, &fit_uname_ramdisk)) {
806 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
807 fit_uname_ramdisk, rd_addr);
808 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100809#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100810 {
811 rd_addr = simple_strtoul(argv[2], NULL, 16);
812 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
813 rd_addr);
814 }
815#if defined(CONFIG_FIT)
816 } else {
817 /* use FIT configuration provided in first bootm
818 * command argument
819 */
820 rd_addr = (ulong)images->fit_hdr_os;
821 fit_uname_config = images->fit_uname_cfg;
822 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
823 fit_uname_config, rd_addr);
824
825 /*
826 * Check whether configuration has ramdisk defined,
827 * if not, don't try to use it, quit silently.
828 */
829 fit_hdr = (void *)rd_addr;
830 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
831 if (cfg_noffset < 0) {
832 debug ("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200833 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100834 }
835
836 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
837 if (rd_noffset < 0) {
838 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500839 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100840 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100841 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100842#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100843
844 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100845 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100846
847 /*
848 * Check if there is an initrd image at the
849 * address provided in the second bootm argument
850 * check image type, for FIT images get FIT node.
851 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100852 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100853 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100854 printf ("## Loading init Ramdisk from Legacy "
855 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100856
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100857 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100858 rd_hdr = image_get_ramdisk (rd_addr, arch,
859 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100860
Marian Balakowiczc8779642008-03-12 10:12:37 +0100861 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600862 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600863
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100864 rd_data = image_get_data (rd_hdr);
865 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100866 rd_load = image_get_load (rd_hdr);
867 break;
868#if defined(CONFIG_FIT)
869 case IMAGE_FORMAT_FIT:
870 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100871 printf ("## Loading init Ramdisk from FIT "
872 "Image at %08lx ...\n", rd_addr);
873
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100874 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100875 if (!fit_check_format (fit_hdr)) {
876 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100877 show_boot_progress (-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200878 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100879 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100880 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100881
882 if (!fit_uname_ramdisk) {
883 /*
884 * no ramdisk image node unit name, try to get config
885 * node first. If config unit node name is NULL
886 * fit_conf_get_node() will try to find default config node
887 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100888 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100889 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
890 if (cfg_noffset < 0) {
891 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100892 show_boot_progress (-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200893 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100894 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100895 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
896 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100897
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100898 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100899 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
900 } else {
901 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100902 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100903 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
904 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100905 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100906 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100907 show_boot_progress (-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200908 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100909 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100910
911 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
912
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100913 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100914 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200915 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100916
917 /* get ramdisk image data address and length */
918 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
919 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100920 show_boot_progress (-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200921 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100922 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100923 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100924
925 rd_data = (ulong)data;
926 rd_len = size;
927
928 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
929 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100930 show_boot_progress (-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200931 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100932 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100933 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100934
935 images->fit_hdr_rd = fit_hdr;
936 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100937 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100938 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100939#endif
940 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100941 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100942 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500943 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100944 }
945
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100946#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100947 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100948 * We need to copy the ramdisk to SRAM to let Linux boot
949 */
950 if (rd_data) {
951 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
952 rd_data = rd_load;
953 }
954#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
955
956 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200957 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100958 /*
959 * Now check if we have a legacy mult-component image,
960 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100961 */
962 show_boot_progress (13);
963 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100964 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100965 (ulong)images->legacy_hdr_os);
966
967 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100968 } else {
969 /*
970 * no initrd image
971 */
972 show_boot_progress (14);
973 rd_len = rd_data = 0;
974 }
975
976 if (!rd_data) {
977 debug ("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100978 } else {
979 *rd_start = rd_data;
980 *rd_end = rd_data + rd_len;
981 }
982 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
983 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -0600984
985 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100986}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100987
Daniel Hellstrombf3d8b32008-03-28 08:29:26 +0100988#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100989/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100990 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -0600991 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100992 * @rd_data: ramdisk data start address
993 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100994 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
995 * start address (after possible relocation)
996 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
997 * end address (after possible relocation)
998 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100999 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001000 * variable and if requested ramdisk data is moved to a specified location.
1001 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001002 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1003 * start/end addresses if ramdisk image start and len were provided,
1004 * otherwise set initrd_start and initrd_end set to zeros.
1005 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001006 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001007 * 0 - success
1008 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001009 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001010int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001011 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001012{
1013 char *s;
1014 ulong initrd_high;
1015 int initrd_copy_to_ram = 1;
1016
1017 if ((s = getenv ("initrd_high")) != NULL) {
1018 /* a value of "no" or a similar string will act like 0,
1019 * turning the "load high" feature off. This is intentional.
1020 */
1021 initrd_high = simple_strtoul (s, NULL, 16);
1022 if (initrd_high == ~0)
1023 initrd_copy_to_ram = 0;
1024 } else {
1025 /* not set, no restrictions to load high */
1026 initrd_high = ~0;
1027 }
1028
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001029
1030#ifdef CONFIG_LOGBUFFER
1031 /* Prevent initrd from overwriting logbuffer */
1032 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1033#endif
1034
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001035 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1036 initrd_high, initrd_copy_to_ram);
1037
1038 if (rd_data) {
1039 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1040 debug (" in-place initrd\n");
1041 *initrd_start = rd_data;
1042 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001043 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001044 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001045 if (initrd_high)
Becky Bruce391fd932008-06-09 20:37:18 -05001046 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001047 else
Becky Bruce391fd932008-06-09 20:37:18 -05001048 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001049
Kumar Galae822d7f2008-02-27 21:51:49 -06001050 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001051 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001052 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001053 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001054 show_boot_progress (12);
1055
1056 *initrd_end = *initrd_start + rd_len;
1057 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1058 *initrd_start, *initrd_end);
1059
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001060 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001061 (void *)rd_data, rd_len, CHUNKSZ);
1062
1063 puts ("OK\n");
1064 }
1065 } else {
1066 *initrd_start = 0;
1067 *initrd_end = 0;
1068 }
1069 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1070 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001071
Kumar Galae822d7f2008-02-27 21:51:49 -06001072 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001073
Kumar Galae822d7f2008-02-27 21:51:49 -06001074error:
1075 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001076}
Jean-Christophe PLAGNIOL-VILLARDcd673452008-11-24 13:33:51 +01001077#endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001078
Kumar Gala06a09912008-08-15 08:24:38 -05001079#ifdef CONFIG_OF_LIBFDT
1080static void fdt_error (const char *msg)
1081{
1082 puts ("ERROR: ");
1083 puts (msg);
1084 puts (" - must RESET the board to recover.\n");
1085}
1086
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001087static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001088{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001089 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001090
1091 image_print_contents (fdt_hdr);
1092
1093 puts (" Verifying Checksum ... ");
1094 if (!image_check_hcrc (fdt_hdr)) {
1095 fdt_error ("fdt header checksum invalid");
1096 return NULL;
1097 }
1098
1099 if (!image_check_dcrc (fdt_hdr)) {
1100 fdt_error ("fdt checksum invalid");
1101 return NULL;
1102 }
1103 puts ("OK\n");
1104
1105 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1106 fdt_error ("uImage is not a fdt");
1107 return NULL;
1108 }
1109 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1110 fdt_error ("uImage is compressed");
1111 return NULL;
1112 }
1113 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1114 fdt_error ("uImage data is not a fdt");
1115 return NULL;
1116 }
1117 return fdt_hdr;
1118}
1119
1120/**
1121 * fit_check_fdt - verify FIT format FDT subimage
1122 * @fit_hdr: pointer to the FIT header
1123 * fdt_noffset: FDT subimage node offset within FIT image
1124 * @verify: data CRC verification flag
1125 *
1126 * fit_check_fdt() verifies integrity of the FDT subimage and from
1127 * specified FIT image.
1128 *
1129 * returns:
1130 * 1, on success
1131 * 0, on failure
1132 */
1133#if defined(CONFIG_FIT)
1134static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1135{
1136 fit_image_print (fit, fdt_noffset, " ");
1137
1138 if (verify) {
1139 puts (" Verifying Hash Integrity ... ");
1140 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1141 fdt_error ("Bad Data Hash");
1142 return 0;
1143 }
1144 puts ("OK\n");
1145 }
1146
1147 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1148 fdt_error ("Not a FDT image");
1149 return 0;
1150 }
1151
1152 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1153 fdt_error ("FDT image is compressed");
1154 return 0;
1155 }
1156
1157 return 1;
1158}
1159#endif /* CONFIG_FIT */
1160
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001161#ifndef CONFIG_SYS_FDT_PAD
1162#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001163#endif
1164
1165/**
1166 * boot_relocate_fdt - relocate flat device tree
1167 * @lmb: pointer to lmb handle, will be used for memory mgmt
1168 * @bootmap_base: base address of the bootmap region
1169 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1170 * @of_size: pointer to a ulong variable, will hold fdt length
1171 *
1172 * boot_relocate_fdt() determines if the of_flat_tree address is within
1173 * the bootmap and if not relocates it into that region
1174 *
1175 * of_flat_tree and of_size are set to final (after relocation) values
1176 *
1177 * returns:
1178 * 0 - success
1179 * 1 - failure
1180 */
1181int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1182 char **of_flat_tree, ulong *of_size)
1183{
1184 char *fdt_blob = *of_flat_tree;
1185 ulong relocate = 0;
1186 ulong of_len = 0;
1187
1188 /* nothing to do */
1189 if (*of_size == 0)
1190 return 0;
1191
1192 if (fdt_check_header (fdt_blob) != 0) {
1193 fdt_error ("image is not a fdt");
1194 goto error;
1195 }
1196
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001197#ifndef CONFIG_SYS_NO_FLASH
Kumar Gala06a09912008-08-15 08:24:38 -05001198 /* move the blob if it is in flash (set relocate) */
1199 if (addr2info ((ulong)fdt_blob) != NULL)
1200 relocate = 1;
1201#endif
1202
1203 /*
1204 * The blob needs to be inside the boot mapping.
1205 */
1206 if (fdt_blob < (char *)bootmap_base)
1207 relocate = 1;
1208
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001209 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1210 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
Kumar Gala06a09912008-08-15 08:24:38 -05001211 relocate = 1;
1212
1213 /* move flattend device tree if needed */
1214 if (relocate) {
1215 int err;
1216 ulong of_start = 0;
1217
1218 /* position on a 4K boundary before the alloc_current */
1219 /* Pad the FDT by a specified amount */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001220 of_len = *of_size + CONFIG_SYS_FDT_PAD;
Kumar Gala06a09912008-08-15 08:24:38 -05001221 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001222 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
Kumar Gala06a09912008-08-15 08:24:38 -05001223
1224 if (of_start == 0) {
1225 puts("device tree - allocation error\n");
1226 goto error;
1227 }
1228
1229 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1230 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1231 of_len, of_len);
1232
1233 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1234 of_start, of_start + of_len - 1);
1235
1236 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1237 if (err != 0) {
1238 fdt_error ("fdt move failed");
1239 goto error;
1240 }
1241 puts ("OK\n");
1242
1243 *of_flat_tree = (char *)of_start;
1244 *of_size = of_len;
1245 } else {
1246 *of_flat_tree = fdt_blob;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001247 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
Kumar Gala06a09912008-08-15 08:24:38 -05001248 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1249 fdt_set_totalsize(*of_flat_tree, of_len);
1250
1251 *of_size = of_len;
1252 }
1253
Kumar Gala54f9c862008-08-15 08:24:39 -05001254 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001255 return 0;
1256
1257error:
1258 return 1;
1259}
1260
1261/**
1262 * boot_get_fdt - main fdt handling routine
1263 * @argc: command argument count
1264 * @argv: command argument list
1265 * @images: pointer to the bootm images structure
1266 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1267 * @of_size: pointer to a ulong variable, will hold fdt length
1268 *
1269 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1270 * Curently supported are the following ramdisk sources:
1271 * - multicomponent kernel/ramdisk image,
1272 * - commandline provided address of decicated ramdisk image.
1273 *
1274 * returns:
1275 * 0, if fdt image was found and valid, or skipped
1276 * of_flat_tree and of_size are set to fdt start address and length if
1277 * fdt image is found and valid
1278 *
1279 * 1, if fdt image is found but corrupted
1280 * of_flat_tree and of_size are set to 0 if no fdt exists
1281 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001282int boot_get_fdt (int flag, int argc, char * const argv[], bootm_headers_t *images,
Kumar Gala06a09912008-08-15 08:24:38 -05001283 char **of_flat_tree, ulong *of_size)
1284{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001285 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001286 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001287 char *fdt_blob = NULL;
1288 ulong image_start, image_end;
1289 ulong load_start, load_end;
1290#if defined(CONFIG_FIT)
1291 void *fit_hdr;
1292 const char *fit_uname_config = NULL;
1293 const char *fit_uname_fdt = NULL;
1294 ulong default_addr;
1295 int cfg_noffset;
1296 int fdt_noffset;
1297 const void *data;
1298 size_t size;
1299#endif
1300
1301 *of_flat_tree = NULL;
1302 *of_size = 0;
1303
1304 if (argc > 3 || genimg_has_config (images)) {
1305#if defined(CONFIG_FIT)
1306 if (argc > 3) {
1307 /*
1308 * If the FDT blob comes from the FIT image and the
1309 * FIT image address is omitted in the command line
1310 * argument, try to use ramdisk or os FIT image
1311 * address or default load address.
1312 */
1313 if (images->fit_uname_rd)
1314 default_addr = (ulong)images->fit_hdr_rd;
1315 else if (images->fit_uname_os)
1316 default_addr = (ulong)images->fit_hdr_os;
1317 else
1318 default_addr = load_addr;
1319
1320 if (fit_parse_conf (argv[3], default_addr,
1321 &fdt_addr, &fit_uname_config)) {
1322 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1323 fit_uname_config, fdt_addr);
1324 } else if (fit_parse_subimage (argv[3], default_addr,
1325 &fdt_addr, &fit_uname_fdt)) {
1326 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1327 fit_uname_fdt, fdt_addr);
1328 } else
1329#endif
1330 {
1331 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1332 debug ("* fdt: cmdline image address = 0x%08lx\n",
1333 fdt_addr);
1334 }
1335#if defined(CONFIG_FIT)
1336 } else {
1337 /* use FIT configuration provided in first bootm
1338 * command argument
1339 */
1340 fdt_addr = (ulong)images->fit_hdr_os;
1341 fit_uname_config = images->fit_uname_cfg;
1342 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1343 fit_uname_config, fdt_addr);
1344
1345 /*
1346 * Check whether configuration has FDT blob defined,
1347 * if not quit silently.
1348 */
1349 fit_hdr = (void *)fdt_addr;
1350 cfg_noffset = fit_conf_get_node (fit_hdr,
1351 fit_uname_config);
1352 if (cfg_noffset < 0) {
1353 debug ("* fdt: no such config\n");
1354 return 0;
1355 }
1356
1357 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1358 cfg_noffset);
1359 if (fdt_noffset < 0) {
1360 debug ("* fdt: no fdt in config\n");
1361 return 0;
1362 }
1363 }
1364#endif
1365
1366 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1367 fdt_addr);
1368
1369 /* copy from dataflash if needed */
1370 fdt_addr = genimg_get_image (fdt_addr);
1371
1372 /*
1373 * Check if there is an FDT image at the
1374 * address provided in the second bootm argument
1375 * check image type, for FIT images get a FIT node.
1376 */
1377 switch (genimg_get_format ((void *)fdt_addr)) {
1378 case IMAGE_FORMAT_LEGACY:
1379 /* verify fdt_addr points to a valid image header */
1380 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1381 fdt_addr);
1382 fdt_hdr = image_get_fdt (fdt_addr);
1383 if (!fdt_hdr)
1384 goto error;
1385
1386 /*
1387 * move image data to the load address,
1388 * make sure we don't overwrite initial image
1389 */
1390 image_start = (ulong)fdt_hdr;
1391 image_end = image_get_image_end (fdt_hdr);
1392
1393 load_start = image_get_load (fdt_hdr);
1394 load_end = load_start + image_get_data_size (fdt_hdr);
1395
1396 if ((load_start < image_end) && (load_end > image_start)) {
1397 fdt_error ("fdt overwritten");
1398 goto error;
1399 }
1400
1401 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1402 image_get_data (fdt_hdr), load_start);
1403
1404 memmove ((void *)load_start,
1405 (void *)image_get_data (fdt_hdr),
1406 image_get_data_size (fdt_hdr));
1407
1408 fdt_blob = (char *)load_start;
1409 break;
1410 case IMAGE_FORMAT_FIT:
1411 /*
1412 * This case will catch both: new uImage format
1413 * (libfdt based) and raw FDT blob (also libfdt
1414 * based).
1415 */
1416#if defined(CONFIG_FIT)
1417 /* check FDT blob vs FIT blob */
1418 if (fit_check_format ((const void *)fdt_addr)) {
1419 /*
1420 * FIT image
1421 */
1422 fit_hdr = (void *)fdt_addr;
1423 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1424 fdt_addr);
1425
1426 if (!fit_uname_fdt) {
1427 /*
1428 * no FDT blob image node unit name,
1429 * try to get config node first. If
1430 * config unit node name is NULL
1431 * fit_conf_get_node() will try to
1432 * find default config node
1433 */
1434 cfg_noffset = fit_conf_get_node (fit_hdr,
1435 fit_uname_config);
1436
1437 if (cfg_noffset < 0) {
1438 fdt_error ("Could not find configuration node\n");
1439 goto error;
1440 }
1441
1442 fit_uname_config = fdt_get_name (fit_hdr,
1443 cfg_noffset, NULL);
1444 printf (" Using '%s' configuration\n",
1445 fit_uname_config);
1446
1447 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1448 cfg_noffset);
1449 fit_uname_fdt = fit_get_name (fit_hdr,
1450 fdt_noffset, NULL);
1451 } else {
1452 /* get FDT component image node offset */
1453 fdt_noffset = fit_image_get_node (fit_hdr,
1454 fit_uname_fdt);
1455 }
1456 if (fdt_noffset < 0) {
1457 fdt_error ("Could not find subimage node\n");
1458 goto error;
1459 }
1460
1461 printf (" Trying '%s' FDT blob subimage\n",
1462 fit_uname_fdt);
1463
1464 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1465 images->verify))
1466 goto error;
1467
1468 /* get ramdisk image data address and length */
1469 if (fit_image_get_data (fit_hdr, fdt_noffset,
1470 &data, &size)) {
1471 fdt_error ("Could not find FDT subimage data");
1472 goto error;
1473 }
1474
1475 /* verift that image data is a proper FDT blob */
1476 if (fdt_check_header ((char *)data) != 0) {
1477 fdt_error ("Subimage data is not a FTD");
1478 goto error;
1479 }
1480
1481 /*
1482 * move image data to the load address,
1483 * make sure we don't overwrite initial image
1484 */
1485 image_start = (ulong)fit_hdr;
1486 image_end = fit_get_end (fit_hdr);
1487
1488 if (fit_image_get_load (fit_hdr, fdt_noffset,
1489 &load_start) == 0) {
1490 load_end = load_start + size;
1491
1492 if ((load_start < image_end) &&
1493 (load_end > image_start)) {
1494 fdt_error ("FDT overwritten");
1495 goto error;
1496 }
1497
1498 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1499 (ulong)data, load_start);
1500
1501 memmove ((void *)load_start,
1502 (void *)data, size);
1503
1504 fdt_blob = (char *)load_start;
1505 } else {
1506 fdt_blob = (char *)data;
1507 }
1508
1509 images->fit_hdr_fdt = fit_hdr;
1510 images->fit_uname_fdt = fit_uname_fdt;
1511 images->fit_noffset_fdt = fdt_noffset;
1512 break;
1513 } else
1514#endif
1515 {
1516 /*
1517 * FDT blob
1518 */
1519 fdt_blob = (char *)fdt_addr;
1520 debug ("* fdt: raw FDT blob\n");
1521 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1522 }
1523 break;
1524 default:
Anatolij Gustschin8e024942008-08-29 21:04:45 +02001525 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001526 goto error;
1527 }
1528
1529 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1530
1531 } else if (images->legacy_hdr_valid &&
1532 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1533
1534 ulong fdt_data, fdt_len;
1535
1536 /*
1537 * Now check if we have a legacy multi-component image,
1538 * get second entry data start address and len.
1539 */
1540 printf ("## Flattened Device Tree from multi "
1541 "component Image at %08lX\n",
1542 (ulong)images->legacy_hdr_os);
1543
1544 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1545 if (fdt_len) {
1546
1547 fdt_blob = (char *)fdt_data;
1548 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1549
1550 if (fdt_check_header (fdt_blob) != 0) {
1551 fdt_error ("image is not a fdt");
1552 goto error;
1553 }
1554
1555 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1556 fdt_error ("fdt size != image size");
1557 goto error;
1558 }
1559 } else {
1560 debug ("## No Flattened Device Tree\n");
1561 return 0;
1562 }
1563 } else {
1564 debug ("## No Flattened Device Tree\n");
1565 return 0;
1566 }
1567
1568 *of_flat_tree = fdt_blob;
1569 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1570 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001571 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001572
1573 return 0;
1574
1575error:
1576 *of_flat_tree = 0;
1577 *of_size = 0;
1578 return 1;
1579}
1580#endif /* CONFIG_OF_LIBFDT */
1581
Jean-Christophe PLAGNIOL-VILLARDcd673452008-11-24 13:33:51 +01001582#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001583/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001584 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001585 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001586 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1587 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galae822d7f2008-02-27 21:51:49 -06001588 * @bootmap_base: ulong variable, holds offset in physical memory to
1589 * base of bootmap
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001590 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001591 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galae822d7f2008-02-27 21:51:49 -06001592 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001593 * variable is present its contents is copied to allocated kernel
1594 * command line.
1595 *
1596 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001597 * 0 - success
1598 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001599 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001600int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galae822d7f2008-02-27 21:51:49 -06001601 ulong bootmap_base)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001602{
1603 char *cmdline;
1604 char *s;
1605
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001606 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1607 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galae822d7f2008-02-27 21:51:49 -06001608
1609 if (cmdline == NULL)
1610 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001611
1612 if ((s = getenv("bootargs")) == NULL)
1613 s = "";
1614
1615 strcpy(cmdline, s);
1616
1617 *cmd_start = (ulong) & cmdline[0];
1618 *cmd_end = *cmd_start + strlen(cmdline);
1619
1620 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1621
Kumar Galae822d7f2008-02-27 21:51:49 -06001622 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001623}
1624
1625/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001626 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001627 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001628 * @kbd: double pointer to board info data
Kumar Galae822d7f2008-02-27 21:51:49 -06001629 * @bootmap_base: ulong variable, holds offset in physical memory to
1630 * base of bootmap
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001631 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001632 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galae822d7f2008-02-27 21:51:49 -06001633 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1634 * the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001635 *
1636 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001637 * 0 - success
1638 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001639 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001640int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001641{
Becky Bruce391fd932008-06-09 20:37:18 -05001642 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001643 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galae822d7f2008-02-27 21:51:49 -06001644 if (*kbd == NULL)
1645 return -1;
1646
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001647 **kbd = *(gd->bd);
1648
1649 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1650
1651#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1652 do_bdinfo(NULL, 0, 0, NULL);
1653#endif
1654
Kumar Galae822d7f2008-02-27 21:51:49 -06001655 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001656}
1657#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001658#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001659
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001660#if defined(CONFIG_FIT)
1661/*****************************************************************************/
1662/* New uImage format routines */
1663/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001664#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001665static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1666 ulong *addr, const char **name)
1667{
1668 const char *sep;
1669
1670 *addr = addr_curr;
1671 *name = NULL;
1672
1673 sep = strchr (spec, sepc);
1674 if (sep) {
1675 if (sep - spec > 0)
1676 *addr = simple_strtoul (spec, NULL, 16);
1677
1678 *name = sep + 1;
1679 return 1;
1680 }
1681
1682 return 0;
1683}
1684
1685/**
1686 * fit_parse_conf - parse FIT configuration spec
1687 * @spec: input string, containing configuration spec
1688 * @add_curr: current image address (to be used as a possible default)
1689 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1690 * configuration
1691 * @conf_name double pointer to a char, will hold pointer to a configuration
1692 * unit name
1693 *
1694 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1695 * where <addr> is a FIT image address that contains configuration
1696 * with a <conf> unit name.
1697 *
1698 * Address part is optional, and if omitted default add_curr will
1699 * be used instead.
1700 *
1701 * returns:
1702 * 1 if spec is a valid configuration string,
1703 * addr and conf_name are set accordingly
1704 * 0 otherwise
1705 */
1706inline int fit_parse_conf (const char *spec, ulong addr_curr,
1707 ulong *addr, const char **conf_name)
1708{
1709 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1710}
1711
1712/**
1713 * fit_parse_subimage - parse FIT subimage spec
1714 * @spec: input string, containing subimage spec
1715 * @add_curr: current image address (to be used as a possible default)
1716 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1717 * subimage
1718 * @image_name: double pointer to a char, will hold pointer to a subimage name
1719 *
1720 * fit_parse_subimage() expects subimage spec in the for of
1721 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1722 * subimage with a <subimg> unit name.
1723 *
1724 * Address part is optional, and if omitted default add_curr will
1725 * be used instead.
1726 *
1727 * returns:
1728 * 1 if spec is a valid subimage string,
1729 * addr and image_name are set accordingly
1730 * 0 otherwise
1731 */
1732inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1733 ulong *addr, const char **image_name)
1734{
1735 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1736}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001737#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001738
1739static void fit_get_debug (const void *fit, int noffset,
1740 char *prop_name, int err)
1741{
1742 debug ("Can't get '%s' property from FIT 0x%08lx, "
1743 "node: offset %d, name %s (%s)\n",
1744 prop_name, (ulong)fit, noffset,
1745 fit_get_name (fit, noffset, NULL),
1746 fdt_strerror (err));
1747}
1748
1749/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001750 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001751 * @fit: pointer to the FIT format image header
1752 * @p: pointer to prefix string
1753 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001754 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001755 * The routine prints out FIT image properties (root node level) follwed by
1756 * the details of each component image.
1757 *
1758 * returns:
1759 * no returned results
1760 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001761void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001762{
1763 char *desc;
1764 char *uname;
1765 int images_noffset;
1766 int confs_noffset;
1767 int noffset;
1768 int ndepth;
1769 int count = 0;
1770 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001771 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001772#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1773 time_t timestamp;
1774#endif
1775
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001776#ifdef USE_HOSTCC
1777 p = "";
1778#else
1779 p = " ";
1780#endif
1781
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001782 /* Root node properties */
1783 ret = fit_get_desc (fit, 0, &desc);
1784 printf ("%sFIT description: ", p);
1785 if (ret)
1786 printf ("unavailable\n");
1787 else
1788 printf ("%s\n", desc);
1789
1790#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1791 ret = fit_get_timestamp (fit, 0, &timestamp);
1792 printf ("%sCreated: ", p);
1793 if (ret)
1794 printf ("unavailable\n");
1795 else
1796 genimg_print_time (timestamp);
1797#endif
1798
1799 /* Find images parent node offset */
1800 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1801 if (images_noffset < 0) {
1802 printf ("Can't find images parent node '%s' (%s)\n",
1803 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1804 return;
1805 }
1806
1807 /* Process its subnodes, print out component images details */
1808 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1809 (noffset >= 0) && (ndepth > 0);
1810 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1811 if (ndepth == 1) {
1812 /*
1813 * Direct child node of the images parent node,
1814 * i.e. component image node.
1815 */
1816 printf ("%s Image %u (%s)\n", p, count++,
1817 fit_get_name(fit, noffset, NULL));
1818
1819 fit_image_print (fit, noffset, p);
1820 }
1821 }
1822
1823 /* Find configurations parent node offset */
1824 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1825 if (confs_noffset < 0) {
1826 debug ("Can't get configurations parent node '%s' (%s)\n",
1827 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1828 return;
1829 }
1830
1831 /* get default configuration unit name from default property */
1832 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1833 if (uname)
1834 printf ("%s Default Configuration: '%s'\n", p, uname);
1835
1836 /* Process its subnodes, print out configurations details */
1837 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1838 (noffset >= 0) && (ndepth > 0);
1839 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1840 if (ndepth == 1) {
1841 /*
1842 * Direct child node of the configurations parent node,
1843 * i.e. configuration node.
1844 */
1845 printf ("%s Configuration %u (%s)\n", p, count++,
1846 fit_get_name(fit, noffset, NULL));
1847
1848 fit_conf_print (fit, noffset, p);
1849 }
1850 }
1851}
1852
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001853/**
1854 * fit_image_print - prints out the FIT component image details
1855 * @fit: pointer to the FIT format image header
1856 * @image_noffset: offset of the component image node
1857 * @p: pointer to prefix string
1858 *
1859 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001860 * image. If present, hash nodes are printed out as well. Load
1861 * address for images of type firmware is also printed out. Since the load
1862 * address is not mandatory for firmware images, it will be output as
1863 * "unavailable" when not present.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001864 *
1865 * returns:
1866 * no returned results
1867 */
1868void fit_image_print (const void *fit, int image_noffset, const char *p)
1869{
1870 char *desc;
1871 uint8_t type, arch, os, comp;
1872 size_t size;
1873 ulong load, entry;
1874 const void *data;
1875 int noffset;
1876 int ndepth;
1877 int ret;
1878
1879 /* Mandatory properties */
1880 ret = fit_get_desc (fit, image_noffset, &desc);
1881 printf ("%s Description: ", p);
1882 if (ret)
1883 printf ("unavailable\n");
1884 else
1885 printf ("%s\n", desc);
1886
1887 fit_image_get_type (fit, image_noffset, &type);
1888 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1889
1890 fit_image_get_comp (fit, image_noffset, &comp);
1891 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1892
1893 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1894
1895#ifndef USE_HOSTCC
1896 printf ("%s Data Start: ", p);
1897 if (ret)
1898 printf ("unavailable\n");
1899 else
1900 printf ("0x%08lx\n", (ulong)data);
1901#endif
1902
1903 printf ("%s Data Size: ", p);
1904 if (ret)
1905 printf ("unavailable\n");
1906 else
1907 genimg_print_size (size);
1908
1909 /* Remaining, type dependent properties */
1910 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1911 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1912 (type == IH_TYPE_FLATDT)) {
1913 fit_image_get_arch (fit, image_noffset, &arch);
1914 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1915 }
1916
1917 if (type == IH_TYPE_KERNEL) {
1918 fit_image_get_os (fit, image_noffset, &os);
1919 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1920 }
1921
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001922 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1923 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001924 ret = fit_image_get_load (fit, image_noffset, &load);
1925 printf ("%s Load Address: ", p);
1926 if (ret)
1927 printf ("unavailable\n");
1928 else
1929 printf ("0x%08lx\n", load);
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001930 }
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001931
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02001932 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001933 fit_image_get_entry (fit, image_noffset, &entry);
1934 printf ("%s Entry Point: ", p);
1935 if (ret)
1936 printf ("unavailable\n");
1937 else
1938 printf ("0x%08lx\n", entry);
1939 }
1940
1941 /* Process all hash subnodes of the component image node */
1942 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1943 (noffset >= 0) && (ndepth > 0);
1944 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1945 if (ndepth == 1) {
1946 /* Direct child node of the component image node */
1947 fit_image_print_hash (fit, noffset, p);
1948 }
1949 }
1950}
1951
1952/**
1953 * fit_image_print_hash - prints out the hash node details
1954 * @fit: pointer to the FIT format image header
1955 * @noffset: offset of the hash node
1956 * @p: pointer to prefix string
1957 *
1958 * fit_image_print_hash() lists properies for the processed hash node
1959 *
1960 * returns:
1961 * no returned results
1962 */
1963void fit_image_print_hash (const void *fit, int noffset, const char *p)
1964{
1965 char *algo;
1966 uint8_t *value;
1967 int value_len;
1968 int i, ret;
1969
1970 /*
1971 * Check subnode name, must be equal to "hash".
1972 * Multiple hash nodes require unique unit node
1973 * names, e.g. hash@1, hash@2, etc.
1974 */
1975 if (strncmp (fit_get_name(fit, noffset, NULL),
1976 FIT_HASH_NODENAME,
1977 strlen(FIT_HASH_NODENAME)) != 0)
1978 return;
1979
1980 debug ("%s Hash node: '%s'\n", p,
1981 fit_get_name (fit, noffset, NULL));
1982
1983 printf ("%s Hash algo: ", p);
1984 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1985 printf ("invalid/unsupported\n");
1986 return;
1987 }
1988 printf ("%s\n", algo);
1989
1990 ret = fit_image_hash_get_value (fit, noffset, &value,
1991 &value_len);
1992 printf ("%s Hash value: ", p);
1993 if (ret) {
1994 printf ("unavailable\n");
1995 } else {
1996 for (i = 0; i < value_len; i++)
1997 printf ("%02x", value[i]);
1998 printf ("\n");
1999 }
2000
2001 debug ("%s Hash len: %d\n", p, value_len);
2002}
2003
2004/**
2005 * fit_get_desc - get node description property
2006 * @fit: pointer to the FIT format image header
2007 * @noffset: node offset
2008 * @desc: double pointer to the char, will hold pointer to the descrption
2009 *
2010 * fit_get_desc() reads description property from a given node, if
2011 * description is found pointer to it is returened in third call argument.
2012 *
2013 * returns:
2014 * 0, on success
2015 * -1, on failure
2016 */
2017int fit_get_desc (const void *fit, int noffset, char **desc)
2018{
2019 int len;
2020
2021 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2022 if (*desc == NULL) {
2023 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2024 return -1;
2025 }
2026
2027 return 0;
2028}
2029
2030/**
2031 * fit_get_timestamp - get node timestamp property
2032 * @fit: pointer to the FIT format image header
2033 * @noffset: node offset
2034 * @timestamp: pointer to the time_t, will hold read timestamp
2035 *
2036 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2037 * is found and has a correct size its value is retured in third call
2038 * argument.
2039 *
2040 * returns:
2041 * 0, on success
2042 * -1, on property read failure
2043 * -2, on wrong timestamp size
2044 */
2045int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2046{
2047 int len;
2048 const void *data;
2049
2050 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2051 if (data == NULL) {
2052 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2053 return -1;
2054 }
2055 if (len != sizeof (uint32_t)) {
2056 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2057 return -2;
2058 }
2059
2060 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2061 return 0;
2062}
2063
2064/**
2065 * fit_image_get_node - get node offset for component image of a given unit name
2066 * @fit: pointer to the FIT format image header
2067 * @image_uname: component image node unit name
2068 *
2069 * fit_image_get_node() finds a component image (withing the '/images'
2070 * node) of a provided unit name. If image is found its node offset is
2071 * returned to the caller.
2072 *
2073 * returns:
2074 * image node offset when found (>=0)
2075 * negative number on failure (FDT_ERR_* code)
2076 */
2077int fit_image_get_node (const void *fit, const char *image_uname)
2078{
2079 int noffset, images_noffset;
2080
2081 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2082 if (images_noffset < 0) {
2083 debug ("Can't find images parent node '%s' (%s)\n",
2084 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2085 return images_noffset;
2086 }
2087
2088 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2089 if (noffset < 0) {
2090 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2091 image_uname, fdt_strerror (noffset));
2092 }
2093
2094 return noffset;
2095}
2096
2097/**
2098 * fit_image_get_os - get os id for a given component image node
2099 * @fit: pointer to the FIT format image header
2100 * @noffset: component image node offset
2101 * @os: pointer to the uint8_t, will hold os numeric id
2102 *
2103 * fit_image_get_os() finds os property in a given component image node.
2104 * If the property is found, its (string) value is translated to the numeric
2105 * id which is returned to the caller.
2106 *
2107 * returns:
2108 * 0, on success
2109 * -1, on failure
2110 */
2111int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2112{
2113 int len;
2114 const void *data;
2115
2116 /* Get OS name from property data */
2117 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2118 if (data == NULL) {
2119 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2120 *os = -1;
2121 return -1;
2122 }
2123
2124 /* Translate OS name to id */
2125 *os = genimg_get_os_id (data);
2126 return 0;
2127}
2128
2129/**
2130 * fit_image_get_arch - get arch id for a given component image node
2131 * @fit: pointer to the FIT format image header
2132 * @noffset: component image node offset
2133 * @arch: pointer to the uint8_t, will hold arch numeric id
2134 *
2135 * fit_image_get_arch() finds arch property in a given component image node.
2136 * If the property is found, its (string) value is translated to the numeric
2137 * id which is returned to the caller.
2138 *
2139 * returns:
2140 * 0, on success
2141 * -1, on failure
2142 */
2143int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2144{
2145 int len;
2146 const void *data;
2147
2148 /* Get architecture name from property data */
2149 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2150 if (data == NULL) {
2151 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2152 *arch = -1;
2153 return -1;
2154 }
2155
2156 /* Translate architecture name to id */
2157 *arch = genimg_get_arch_id (data);
2158 return 0;
2159}
2160
2161/**
2162 * fit_image_get_type - get type id for a given component image node
2163 * @fit: pointer to the FIT format image header
2164 * @noffset: component image node offset
2165 * @type: pointer to the uint8_t, will hold type numeric id
2166 *
2167 * fit_image_get_type() finds type property in a given component image node.
2168 * If the property is found, its (string) value is translated to the numeric
2169 * id which is returned to the caller.
2170 *
2171 * returns:
2172 * 0, on success
2173 * -1, on failure
2174 */
2175int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2176{
2177 int len;
2178 const void *data;
2179
2180 /* Get image type name from property data */
2181 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2182 if (data == NULL) {
2183 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2184 *type = -1;
2185 return -1;
2186 }
2187
2188 /* Translate image type name to id */
2189 *type = genimg_get_type_id (data);
2190 return 0;
2191}
2192
2193/**
2194 * fit_image_get_comp - get comp id for a given component image node
2195 * @fit: pointer to the FIT format image header
2196 * @noffset: component image node offset
2197 * @comp: pointer to the uint8_t, will hold comp numeric id
2198 *
2199 * fit_image_get_comp() finds comp property in a given component image node.
2200 * If the property is found, its (string) value is translated to the numeric
2201 * id which is returned to the caller.
2202 *
2203 * returns:
2204 * 0, on success
2205 * -1, on failure
2206 */
2207int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2208{
2209 int len;
2210 const void *data;
2211
2212 /* Get compression name from property data */
2213 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2214 if (data == NULL) {
2215 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2216 *comp = -1;
2217 return -1;
2218 }
2219
2220 /* Translate compression name to id */
2221 *comp = genimg_get_comp_id (data);
2222 return 0;
2223}
2224
2225/**
2226 * fit_image_get_load - get load address property for a given component image node
2227 * @fit: pointer to the FIT format image header
2228 * @noffset: component image node offset
2229 * @load: pointer to the uint32_t, will hold load address
2230 *
2231 * fit_image_get_load() finds load address property in a given component image node.
2232 * If the property is found, its value is returned to the caller.
2233 *
2234 * returns:
2235 * 0, on success
2236 * -1, on failure
2237 */
2238int fit_image_get_load (const void *fit, int noffset, ulong *load)
2239{
2240 int len;
2241 const uint32_t *data;
2242
2243 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2244 if (data == NULL) {
2245 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2246 return -1;
2247 }
2248
2249 *load = uimage_to_cpu (*data);
2250 return 0;
2251}
2252
2253/**
2254 * fit_image_get_entry - get entry point address property for a given component image node
2255 * @fit: pointer to the FIT format image header
2256 * @noffset: component image node offset
2257 * @entry: pointer to the uint32_t, will hold entry point address
2258 *
2259 * fit_image_get_entry() finds entry point address property in a given component image node.
2260 * If the property is found, its value is returned to the caller.
2261 *
2262 * returns:
2263 * 0, on success
2264 * -1, on failure
2265 */
2266int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2267{
2268 int len;
2269 const uint32_t *data;
2270
2271 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2272 if (data == NULL) {
2273 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2274 return -1;
2275 }
2276
2277 *entry = uimage_to_cpu (*data);
2278 return 0;
2279}
2280
2281/**
2282 * fit_image_get_data - get data property and its size for a given component image node
2283 * @fit: pointer to the FIT format image header
2284 * @noffset: component image node offset
2285 * @data: double pointer to void, will hold data property's data address
2286 * @size: pointer to size_t, will hold data property's data size
2287 *
2288 * fit_image_get_data() finds data property in a given component image node.
2289 * If the property is found its data start address and size are returned to
2290 * the caller.
2291 *
2292 * returns:
2293 * 0, on success
2294 * -1, on failure
2295 */
2296int fit_image_get_data (const void *fit, int noffset,
2297 const void **data, size_t *size)
2298{
2299 int len;
2300
2301 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2302 if (*data == NULL) {
2303 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2304 *size = 0;
2305 return -1;
2306 }
2307
2308 *size = len;
2309 return 0;
2310}
2311
2312/**
2313 * fit_image_hash_get_algo - get hash algorithm name
2314 * @fit: pointer to the FIT format image header
2315 * @noffset: hash node offset
2316 * @algo: double pointer to char, will hold pointer to the algorithm name
2317 *
2318 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2319 * If the property is found its data start address is returned to the caller.
2320 *
2321 * returns:
2322 * 0, on success
2323 * -1, on failure
2324 */
2325int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2326{
2327 int len;
2328
2329 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2330 if (*algo == NULL) {
2331 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2332 return -1;
2333 }
2334
2335 return 0;
2336}
2337
2338/**
2339 * fit_image_hash_get_value - get hash value and length
2340 * @fit: pointer to the FIT format image header
2341 * @noffset: hash node offset
2342 * @value: double pointer to uint8_t, will hold address of a hash value data
2343 * @value_len: pointer to an int, will hold hash data length
2344 *
2345 * fit_image_hash_get_value() finds hash value property in a given hash node.
2346 * If the property is found its data start address and size are returned to
2347 * the caller.
2348 *
2349 * returns:
2350 * 0, on success
2351 * -1, on failure
2352 */
2353int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2354 int *value_len)
2355{
2356 int len;
2357
2358 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2359 if (*value == NULL) {
2360 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2361 *value_len = 0;
2362 return -1;
2363 }
2364
2365 *value_len = len;
2366 return 0;
2367}
2368
2369/**
2370 * fit_set_timestamp - set node timestamp property
2371 * @fit: pointer to the FIT format image header
2372 * @noffset: node offset
2373 * @timestamp: timestamp value to be set
2374 *
2375 * fit_set_timestamp() attempts to set timestamp property in the requested
2376 * node and returns operation status to the caller.
2377 *
2378 * returns:
2379 * 0, on success
2380 * -1, on property read failure
2381 */
2382int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2383{
2384 uint32_t t;
2385 int ret;
2386
2387 t = cpu_to_uimage (timestamp);
2388 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2389 sizeof (uint32_t));
2390 if (ret) {
2391 printf ("Can't set '%s' property for '%s' node (%s)\n",
2392 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2393 fdt_strerror (ret));
2394 return -1;
2395 }
2396
2397 return 0;
2398}
2399
2400/**
2401 * calculate_hash - calculate and return hash for provided input data
2402 * @data: pointer to the input data
2403 * @data_len: data length
2404 * @algo: requested hash algorithm
2405 * @value: pointer to the char, will hold hash value data (caller must
2406 * allocate enough free space)
2407 * value_len: length of the calculated hash
2408 *
2409 * calculate_hash() computes input data hash according to the requested algorithm.
2410 * Resulting hash value is placed in caller provided 'value' buffer, length
2411 * of the calculated hash is returned via value_len pointer argument.
2412 *
2413 * returns:
2414 * 0, on success
2415 * -1, when algo is unsupported
2416 */
2417static int calculate_hash (const void *data, int data_len, const char *algo,
2418 uint8_t *value, int *value_len)
2419{
2420 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002421 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2422 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002423 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2424 *value_len = 4;
2425 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002426 sha1_csum_wd ((unsigned char *) data, data_len,
2427 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002428 *value_len = 20;
2429 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002430 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002431 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002432 } else {
2433 debug ("Unsupported hash alogrithm\n");
2434 return -1;
2435 }
2436 return 0;
2437}
2438
2439#ifdef USE_HOSTCC
2440/**
2441 * fit_set_hashes - process FIT component image nodes and calculate hashes
2442 * @fit: pointer to the FIT format image header
2443 *
2444 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2445 * Hashes are calculated for all component images which have hash subnodes
2446 * with algorithm property set to one of the supported hash algorithms.
2447 *
2448 * returns
2449 * 0, on success
2450 * libfdt error code, on failure
2451 */
2452int fit_set_hashes (void *fit)
2453{
2454 int images_noffset;
2455 int noffset;
2456 int ndepth;
2457 int ret;
2458
2459 /* Find images parent node offset */
2460 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2461 if (images_noffset < 0) {
2462 printf ("Can't find images parent node '%s' (%s)\n",
2463 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2464 return images_noffset;
2465 }
2466
2467 /* Process its subnodes, print out component images details */
2468 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2469 (noffset >= 0) && (ndepth > 0);
2470 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2471 if (ndepth == 1) {
2472 /*
2473 * Direct child node of the images parent node,
2474 * i.e. component image node.
2475 */
2476 ret = fit_image_set_hashes (fit, noffset);
2477 if (ret)
2478 return ret;
2479 }
2480 }
2481
2482 return 0;
2483}
2484
2485/**
2486 * fit_image_set_hashes - calculate/set hashes for given component image node
2487 * @fit: pointer to the FIT format image header
2488 * @image_noffset: requested component image node
2489 *
2490 * fit_image_set_hashes() adds hash values for an component image node. All
2491 * existing hash subnodes are checked, if algorithm property is set to one of
2492 * the supported hash algorithms, hash value is computed and corresponding
2493 * hash node property is set, for example:
2494 *
2495 * Input component image node structure:
2496 *
2497 * o image@1 (at image_noffset)
2498 * | - data = [binary data]
2499 * o hash@1
2500 * |- algo = "sha1"
2501 *
2502 * Output component image node structure:
2503 *
2504 * o image@1 (at image_noffset)
2505 * | - data = [binary data]
2506 * o hash@1
2507 * |- algo = "sha1"
2508 * |- value = sha1(data)
2509 *
2510 * returns:
2511 * 0 on sucess
2512 * <0 on failure
2513 */
2514int fit_image_set_hashes (void *fit, int image_noffset)
2515{
2516 const void *data;
2517 size_t size;
2518 char *algo;
2519 uint8_t value[FIT_MAX_HASH_LEN];
2520 int value_len;
2521 int noffset;
2522 int ndepth;
2523
2524 /* Get image data and data length */
2525 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2526 printf ("Can't get image data/size\n");
2527 return -1;
2528 }
2529
2530 /* Process all hash subnodes of the component image node */
2531 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2532 (noffset >= 0) && (ndepth > 0);
2533 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2534 if (ndepth == 1) {
2535 /* Direct child node of the component image node */
2536
2537 /*
2538 * Check subnode name, must be equal to "hash".
2539 * Multiple hash nodes require unique unit node
2540 * names, e.g. hash@1, hash@2, etc.
2541 */
2542 if (strncmp (fit_get_name(fit, noffset, NULL),
2543 FIT_HASH_NODENAME,
2544 strlen(FIT_HASH_NODENAME)) != 0) {
2545 /* Not a hash subnode, skip it */
2546 continue;
2547 }
2548
2549 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2550 printf ("Can't get hash algo property for "
2551 "'%s' hash node in '%s' image node\n",
2552 fit_get_name (fit, noffset, NULL),
2553 fit_get_name (fit, image_noffset, NULL));
2554 return -1;
2555 }
2556
2557 if (calculate_hash (data, size, algo, value, &value_len)) {
2558 printf ("Unsupported hash algorithm (%s) for "
2559 "'%s' hash node in '%s' image node\n",
2560 algo, fit_get_name (fit, noffset, NULL),
2561 fit_get_name (fit, image_noffset, NULL));
2562 return -1;
2563 }
2564
2565 if (fit_image_hash_set_value (fit, noffset, value,
2566 value_len)) {
2567 printf ("Can't set hash value for "
2568 "'%s' hash node in '%s' image node\n",
2569 fit_get_name (fit, noffset, NULL),
2570 fit_get_name (fit, image_noffset, NULL));
2571 return -1;
2572 }
2573 }
2574 }
2575
2576 return 0;
2577}
2578
2579/**
2580 * fit_image_hash_set_value - set hash value in requested has node
2581 * @fit: pointer to the FIT format image header
2582 * @noffset: hash node offset
2583 * @value: hash value to be set
2584 * @value_len: hash value length
2585 *
2586 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2587 * given and returns operation status to the caller.
2588 *
2589 * returns
2590 * 0, on success
2591 * -1, on failure
2592 */
2593int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2594 int value_len)
2595{
2596 int ret;
2597
2598 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2599 if (ret) {
2600 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2601 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2602 fdt_strerror (ret));
2603 return -1;
2604 }
2605
2606 return 0;
2607}
2608#endif /* USE_HOSTCC */
2609
2610/**
2611 * fit_image_check_hashes - verify data intergity
2612 * @fit: pointer to the FIT format image header
2613 * @image_noffset: component image node offset
2614 *
2615 * fit_image_check_hashes() goes over component image hash nodes,
2616 * re-calculates each data hash and compares with the value stored in hash
2617 * node.
2618 *
2619 * returns:
2620 * 1, if all hashes are valid
2621 * 0, otherwise (or on error)
2622 */
2623int fit_image_check_hashes (const void *fit, int image_noffset)
2624{
2625 const void *data;
2626 size_t size;
2627 char *algo;
2628 uint8_t *fit_value;
2629 int fit_value_len;
2630 uint8_t value[FIT_MAX_HASH_LEN];
2631 int value_len;
2632 int noffset;
2633 int ndepth;
2634 char *err_msg = "";
2635
2636 /* Get image data and data length */
2637 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2638 printf ("Can't get image data/size\n");
2639 return 0;
2640 }
2641
2642 /* Process all hash subnodes of the component image node */
2643 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2644 (noffset >= 0) && (ndepth > 0);
2645 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2646 if (ndepth == 1) {
2647 /* Direct child node of the component image node */
2648
2649 /*
2650 * Check subnode name, must be equal to "hash".
2651 * Multiple hash nodes require unique unit node
2652 * names, e.g. hash@1, hash@2, etc.
2653 */
2654 if (strncmp (fit_get_name(fit, noffset, NULL),
2655 FIT_HASH_NODENAME,
2656 strlen(FIT_HASH_NODENAME)) != 0)
2657 continue;
2658
2659 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002660 err_msg = " error!\nCan't get hash algo "
2661 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002662 goto error;
2663 }
2664 printf ("%s", algo);
2665
2666 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2667 &fit_value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002668 err_msg = " error!\nCan't get hash value "
2669 "property";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002670 goto error;
2671 }
2672
2673 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002674 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002675 goto error;
2676 }
2677
2678 if (value_len != fit_value_len) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002679 err_msg = " error !\nBad hash value len";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002680 goto error;
2681 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002682 err_msg = " error!\nBad hash value";
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002683 goto error;
2684 }
2685 printf ("+ ");
2686 }
2687 }
2688
2689 return 1;
2690
2691error:
2692 printf ("%s for '%s' hash node in '%s' image node\n",
2693 err_msg, fit_get_name (fit, noffset, NULL),
2694 fit_get_name (fit, image_noffset, NULL));
2695 return 0;
2696}
2697
2698/**
Bartlomiej Sieka919f5502008-09-09 12:58:15 +02002699 * fit_all_image_check_hashes - verify data intergity for all images
2700 * @fit: pointer to the FIT format image header
2701 *
2702 * fit_all_image_check_hashes() goes over all images in the FIT and
2703 * for every images checks if all it's hashes are valid.
2704 *
2705 * returns:
2706 * 1, if all hashes of all images are valid
2707 * 0, otherwise (or on error)
2708 */
2709int fit_all_image_check_hashes (const void *fit)
2710{
2711 int images_noffset;
2712 int noffset;
2713 int ndepth;
2714 int count;
2715
2716 /* Find images parent node offset */
2717 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2718 if (images_noffset < 0) {
2719 printf ("Can't find images parent node '%s' (%s)\n",
2720 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2721 return 0;
2722 }
2723
2724 /* Process all image subnodes, check hashes for each */
2725 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2726 (ulong)fit);
2727 for (ndepth = 0, count = 0,
2728 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2729 (noffset >= 0) && (ndepth > 0);
2730 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2731 if (ndepth == 1) {
2732 /*
2733 * Direct child node of the images parent node,
2734 * i.e. component image node.
2735 */
2736 printf (" Hash(es) for Image %u (%s): ", count++,
2737 fit_get_name (fit, noffset, NULL));
2738
2739 if (!fit_image_check_hashes (fit, noffset))
2740 return 0;
2741 printf ("\n");
2742 }
2743 }
2744 return 1;
2745}
2746
2747/**
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002748 * fit_image_check_os - check whether image node is of a given os type
2749 * @fit: pointer to the FIT format image header
2750 * @noffset: component image node offset
2751 * @os: requested image os
2752 *
2753 * fit_image_check_os() reads image os property and compares its numeric
2754 * id with the requested os. Comparison result is returned to the caller.
2755 *
2756 * returns:
2757 * 1 if image is of given os type
2758 * 0 otherwise (or on error)
2759 */
2760int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2761{
2762 uint8_t image_os;
2763
2764 if (fit_image_get_os (fit, noffset, &image_os))
2765 return 0;
2766 return (os == image_os);
2767}
2768
2769/**
2770 * fit_image_check_arch - check whether image node is of a given arch
2771 * @fit: pointer to the FIT format image header
2772 * @noffset: component image node offset
2773 * @arch: requested imagearch
2774 *
2775 * fit_image_check_arch() reads image arch property and compares its numeric
2776 * id with the requested arch. Comparison result is returned to the caller.
2777 *
2778 * returns:
2779 * 1 if image is of given arch
2780 * 0 otherwise (or on error)
2781 */
2782int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2783{
2784 uint8_t image_arch;
2785
2786 if (fit_image_get_arch (fit, noffset, &image_arch))
2787 return 0;
2788 return (arch == image_arch);
2789}
2790
2791/**
2792 * fit_image_check_type - check whether image node is of a given type
2793 * @fit: pointer to the FIT format image header
2794 * @noffset: component image node offset
2795 * @type: requested image type
2796 *
2797 * fit_image_check_type() reads image type property and compares its numeric
2798 * id with the requested type. Comparison result is returned to the caller.
2799 *
2800 * returns:
2801 * 1 if image is of given type
2802 * 0 otherwise (or on error)
2803 */
2804int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2805{
2806 uint8_t image_type;
2807
2808 if (fit_image_get_type (fit, noffset, &image_type))
2809 return 0;
2810 return (type == image_type);
2811}
2812
2813/**
2814 * fit_image_check_comp - check whether image node uses given compression
2815 * @fit: pointer to the FIT format image header
2816 * @noffset: component image node offset
2817 * @comp: requested image compression type
2818 *
2819 * fit_image_check_comp() reads image compression property and compares its
2820 * numeric id with the requested compression type. Comparison result is
2821 * returned to the caller.
2822 *
2823 * returns:
2824 * 1 if image uses requested compression
2825 * 0 otherwise (or on error)
2826 */
2827int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2828{
2829 uint8_t image_comp;
2830
2831 if (fit_image_get_comp (fit, noffset, &image_comp))
2832 return 0;
2833 return (comp == image_comp);
2834}
2835
2836/**
2837 * fit_check_format - sanity check FIT image format
2838 * @fit: pointer to the FIT format image header
2839 *
2840 * fit_check_format() runs a basic sanity FIT image verification.
2841 * Routine checks for mandatory properties, nodes, etc.
2842 *
2843 * returns:
2844 * 1, on success
2845 * 0, on failure
2846 */
2847int fit_check_format (const void *fit)
2848{
2849 /* mandatory / node 'description' property */
2850 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2851 debug ("Wrong FIT format: no description\n");
2852 return 0;
2853 }
2854
2855#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2856 /* mandatory / node 'timestamp' property */
2857 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Siekafbc87dc2008-10-01 15:26:32 +02002858 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002859 return 0;
2860 }
2861#endif
2862
2863 /* mandatory subimages parent '/images' node */
2864 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2865 debug ("Wrong FIT format: no images parent node\n");
2866 return 0;
2867 }
2868
2869 return 1;
2870}
2871
2872/**
2873 * fit_conf_get_node - get node offset for configuration of a given unit name
2874 * @fit: pointer to the FIT format image header
2875 * @conf_uname: configuration node unit name
2876 *
2877 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2878 * parant node) of a provided unit name. If configuration is found its node offset
2879 * is returned to the caller.
2880 *
2881 * When NULL is provided in second argument fit_conf_get_node() will search
2882 * for a default configuration node instead. Default configuration node unit name
2883 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2884 *
2885 * returns:
2886 * configuration node offset when found (>=0)
2887 * negative number on failure (FDT_ERR_* code)
2888 */
2889int fit_conf_get_node (const void *fit, const char *conf_uname)
2890{
2891 int noffset, confs_noffset;
2892 int len;
2893
2894 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2895 if (confs_noffset < 0) {
2896 debug ("Can't find configurations parent node '%s' (%s)\n",
2897 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2898 return confs_noffset;
2899 }
2900
2901 if (conf_uname == NULL) {
2902 /* get configuration unit name from the default property */
2903 debug ("No configuration specified, trying default...\n");
2904 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2905 if (conf_uname == NULL) {
2906 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2907 return len;
2908 }
2909 debug ("Found default configuration: '%s'\n", conf_uname);
2910 }
2911
2912 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2913 if (noffset < 0) {
2914 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2915 conf_uname, fdt_strerror (noffset));
2916 }
2917
2918 return noffset;
2919}
2920
2921static int __fit_conf_get_prop_node (const void *fit, int noffset,
2922 const char *prop_name)
2923{
2924 char *uname;
2925 int len;
2926
2927 /* get kernel image unit name from configuration kernel property */
2928 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2929 if (uname == NULL)
2930 return len;
2931
2932 return fit_image_get_node (fit, uname);
2933}
2934
2935/**
2936 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2937 * a given configuration
2938 * @fit: pointer to the FIT format image header
2939 * @noffset: configuration node offset
2940 *
2941 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2942 * configuration FIT_KERNEL_PROP property and translates it to the node
2943 * offset.
2944 *
2945 * returns:
2946 * image node offset when found (>=0)
2947 * negative number on failure (FDT_ERR_* code)
2948 */
2949int fit_conf_get_kernel_node (const void *fit, int noffset)
2950{
2951 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2952}
2953
2954/**
2955 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2956 * a given configuration
2957 * @fit: pointer to the FIT format image header
2958 * @noffset: configuration node offset
2959 *
2960 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2961 * configuration FIT_KERNEL_PROP property and translates it to the node
2962 * offset.
2963 *
2964 * returns:
2965 * image node offset when found (>=0)
2966 * negative number on failure (FDT_ERR_* code)
2967 */
2968int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2969{
2970 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2971}
2972
2973/**
2974 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2975 * a given configuration
2976 * @fit: pointer to the FIT format image header
2977 * @noffset: configuration node offset
2978 *
2979 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2980 * configuration FIT_KERNEL_PROP property and translates it to the node
2981 * offset.
2982 *
2983 * returns:
2984 * image node offset when found (>=0)
2985 * negative number on failure (FDT_ERR_* code)
2986 */
2987int fit_conf_get_fdt_node (const void *fit, int noffset)
2988{
2989 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2990}
2991
2992/**
2993 * fit_conf_print - prints out the FIT configuration details
2994 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01002995 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002996 * @p: pointer to prefix string
2997 *
2998 * fit_conf_print() lists all mandatory properies for the processed
2999 * configuration node.
3000 *
3001 * returns:
3002 * no returned results
3003 */
3004void fit_conf_print (const void *fit, int noffset, const char *p)
3005{
3006 char *desc;
3007 char *uname;
3008 int ret;
3009
3010 /* Mandatory properties */
3011 ret = fit_get_desc (fit, noffset, &desc);
3012 printf ("%s Description: ", p);
3013 if (ret)
3014 printf ("unavailable\n");
3015 else
3016 printf ("%s\n", desc);
3017
3018 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3019 printf ("%s Kernel: ", p);
3020 if (uname == NULL)
3021 printf ("unavailable\n");
3022 else
3023 printf ("%s\n", uname);
3024
3025 /* Optional properties */
3026 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3027 if (uname)
3028 printf ("%s Init Ramdisk: %s\n", p, uname);
3029
3030 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3031 if (uname)
3032 printf ("%s FDT: %s\n", p, uname);
3033}
Marian Balakowiczc8779642008-03-12 10:12:37 +01003034
3035/**
3036 * fit_check_ramdisk - verify FIT format ramdisk subimage
3037 * @fit_hdr: pointer to the FIT ramdisk header
3038 * @rd_noffset: ramdisk subimage node offset within FIT image
3039 * @arch: requested ramdisk image architecture type
3040 * @verify: data CRC verification flag
3041 *
3042 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3043 * specified FIT image.
3044 *
3045 * returns:
3046 * 1, on success
3047 * 0, on failure
3048 */
3049#ifndef USE_HOSTCC
3050static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3051{
3052 fit_image_print (fit, rd_noffset, " ");
3053
3054 if (verify) {
3055 puts (" Verifying Hash Integrity ... ");
3056 if (!fit_image_check_hashes (fit, rd_noffset)) {
3057 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003058 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003059 return 0;
3060 }
3061 puts ("OK\n");
3062 }
3063
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003064 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003065 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3066 !fit_image_check_arch (fit, rd_noffset, arch) ||
3067 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3068 printf ("No Linux %s Ramdisk Image\n",
3069 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003070 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003071 return 0;
3072 }
3073
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003074 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003075 return 1;
3076}
3077#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003078#endif /* CONFIG_FIT */