blob: 82e7aa4361f3689cdfe69f55d6a3fba01a709a56 [file] [log] [blame]
Marian Balakowicz41d71ed2008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
Marian Balakowicz9c701e82008-01-31 13:57:17 +010025
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +010027#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
Marian Balakowicz902ce702008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicza1cc1472008-02-21 17:27:41 +010042#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
Marian Balakowicze227fbb2008-02-29 21:24:06 +010046#include <image.h>
47
Marian Balakowicza50d5152008-03-12 10:12:37 +010048#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010049#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010052#endif
53
54#if defined(CONFIG_FIT)
Andy Fleming72c23be2008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010056#include <sha1.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010057
58static int fit_check_ramdisk (const void *fit, int os_noffset,
59 uint8_t arch, int verify);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010060#endif
61
Marian Balakowicz2efc2642008-01-31 13:58:13 +010062#ifdef CONFIG_CMD_BDI
63extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
64#endif
65
66DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz61fde552008-02-27 11:01:04 +010067
Wolfgang Denk3b506772009-08-19 11:42:56 +020068static const image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +010069 int verify);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010070#else
Marian Balakowicz95418502008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming72c23be2008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010076
Marian Balakowiczc3c7eb22008-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", },
87 { IH_ARCH_NIOS, "nios", "NIOS", },
88 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Erickson80f51b82008-05-04 16:45:01 -070089 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010090 { IH_ARCH_PPC, "ppc", "PowerPC", },
91 { IH_ARCH_S390, "s390", "IBM S390", },
92 { IH_ARCH_SH, "sh", "SuperH", },
93 { IH_ARCH_SPARC, "sparc", "SPARC", },
94 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
95 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
96 { IH_ARCH_AVR32, "avr32", "AVR32", },
97 { -1, "", "", },
98};
99
100static table_entry_t uimage_os[] = {
101 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100102 { IH_OS_LINUX, "linux", "Linux", },
103#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
104 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
105#endif
106 { IH_OS_NETBSD, "netbsd", "NetBSD", },
107 { IH_OS_RTEMS, "rtems", "RTEMS", },
108 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
109#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
110 { IH_OS_QNX, "qnx", "QNX", },
111 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
112#endif
Peter Tyser56b8dd12008-09-08 14:56:49 -0500113#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
114 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
115#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100116#ifdef USE_HOSTCC
117 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
118 { IH_OS_DELL, "dell", "Dell", },
119 { IH_OS_ESIX, "esix", "Esix", },
120 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
121 { IH_OS_IRIX, "irix", "Irix", },
122 { IH_OS_NCR, "ncr", "NCR", },
123 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
124 { IH_OS_PSOS, "psos", "pSOS", },
125 { IH_OS_SCO, "sco", "SCO", },
126 { IH_OS_SOLARIS, "solaris", "Solaris", },
127 { IH_OS_SVR4, "svr4", "SVR4", },
128#endif
129 { -1, "", "", },
130};
131
132static table_entry_t uimage_type[] = {
133 { IH_TYPE_INVALID, NULL, "Invalid Image", },
134 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
135 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
136 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
137 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
138 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
139 { IH_TYPE_SCRIPT, "script", "Script", },
140 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
141 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Prafulla Wadaskar07329412009-09-07 15:05:02 +0530142 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
Marian Balakowiczc3c7eb22008-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' Mantellini35afc062008-09-08 02:46:13 +0200150 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100151 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100152 { -1, "", "", },
153};
154
Mike Frysingera2c67e92008-03-31 11:02:01 -0400155uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200156uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowiczc3c7eb22008-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 Balakowicz41d71ed2008-01-08 18:14:09 +0100160
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100161/*****************************************************************************/
162/* Legacy format routines */
163/*****************************************************************************/
Wolfgang Denk3b506772009-08-19 11:42:56 +0200164int image_check_hcrc (const image_header_t *hdr)
Marian Balakowicz41d71ed2008-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 Denk3b506772009-08-19 11:42:56 +0200179int image_check_dcrc (const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100180{
181 ulong data = image_get_data (hdr);
182 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200183 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100184
185 return (dcrc == image_get_dcrc (hdr));
186}
187
Marian Balakowiczb4a12a92008-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 Denk3b506772009-08-19 11:42:56 +0200201ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100202{
203 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100204 uint32_t *size;
Marian Balakowiczb4a12a92008-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 Balakowicza1474212008-02-29 16:00:06 +0100208 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-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 Denk3b506772009-08-19 11:42:56 +0200234void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100235 ulong *data, ulong *len)
236{
237 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100238 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700239 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-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 Balakowicza1474212008-02-29 16:00:06 +0100246 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-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 Balakowicza1474212008-02-29 16:00:06 +0100250 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100251
252 if (idx < count) {
Marian Balakowicza1474212008-02-29 16:00:06 +0100253 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100254 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100255
256 /* go over all indices preceding requested component idx */
257 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-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 Balakowiczb4a12a92008-01-08 18:12:17 +0100260 }
261
262 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700263 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100264 } else {
265 *len = 0;
266 *data = 0;
267 }
268}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100269
Wolfgang Denk3b506772009-08-19 11:42:56 +0200270static void image_print_type (const image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100271{
272 const char *os, *arch, *type, *comp;
273
274 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));
278
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100279 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100280}
281
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100282/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200283 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3b506772009-08-19 11:42:56 +0200284 * @ptr: pointer to the legacy format image header
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100285 * @p: pointer to prefix string
286 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200287 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-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 Denk3b506772009-08-19 11:42:56 +0200294void image_print_contents (const void *ptr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100295{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200296 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200297 const char *p;
298
299#ifdef USE_HOSTCC
300 p = "";
301#else
302 p = " ";
303#endif
304
Marian Balakowiczc3c7eb22008-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100309#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100310 printf ("%sImage Type: ", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100311 image_print_type (hdr);
Marian Balakowiczc3c7eb22008-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100316
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100317 if (image_check_type (hdr, IH_TYPE_MULTI) ||
318 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100319 int i;
320 ulong data, len;
321 ulong count = image_multi_count (hdr);
322
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100323 printf ("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100324 for (i = 0; i < count; i++) {
325 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100338 }
339 }
340}
341
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100342
343#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100344/**
345 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +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
360 * otherwise, return NULL
361 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200362static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100363 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100364{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200365 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100366
367 if (!image_check_magic (rd_hdr)) {
368 puts ("Bad Magic Number\n");
369 show_boot_progress (-10);
370 return NULL;
371 }
372
373 if (!image_check_hcrc (rd_hdr)) {
374 puts ("Bad Header Checksum\n");
375 show_boot_progress (-11);
376 return NULL;
377 }
378
379 show_boot_progress (10);
380 image_print_contents (rd_hdr);
381
382 if (verify) {
383 puts(" Verifying Checksum ... ");
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200384 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100385 puts ("Bad Data CRC\n");
386 show_boot_progress (-12);
387 return NULL;
388 }
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",
398 genimg_get_arch_name(arch));
399 show_boot_progress (-13);
400 return NULL;
401 }
402
403 return rd_hdr;
404}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100405#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100406
407/*****************************************************************************/
408/* Shared dual-format routines */
409/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100410#ifndef USE_HOSTCC
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200411int getenv_yesno (char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100412{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200413 char *s = getenv (var);
Marian Balakowiczd7c88a42008-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-VILLARD03836942008-10-16 15:01:15 +0200425#if defined(CONFIG_SYS_SDRAM_BASE)
426 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100427#elif defined(CONFIG_ARM)
428 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100429#else
430 return 0;
431#endif
432}
433
Becky Bruced26d67c2008-06-09 20:37:18 -0500434phys_size_t getenv_bootm_size(void)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100435{
436 char *s = getenv ("bootm_size");
437 if (s) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500438 phys_size_t tmp;
Becky Bruced26d67c2008-06-09 20:37:18 -0500439 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100440 return tmp;
441 }
442
Marian Balakowicz2499b042008-03-12 12:14:15 +0100443#if defined(CONFIG_ARM)
444 return gd->bd->bi_dram[0].size;
445#else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100446 return gd->bd->bi_memsize;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100447#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100448}
449
450void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
451{
452#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
453 while (len > 0) {
454 size_t tail = (len > chunksz) ? chunksz : len;
455 WATCHDOG_RESET ();
456 memmove (to, from, tail);
457 to += tail;
458 from += tail;
459 len -= tail;
460 }
461#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
462 memmove (to, from, len);
463#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
464}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100465#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100466
Prafulla Wadaskar49432ab2009-09-07 14:59:07 +0530467void genimg_print_size (uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100468{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100469#ifndef USE_HOSTCC
470 printf ("%d Bytes = ", size);
471 print_size (size, "\n");
472#else
473 printf ("%d Bytes = %.2f kB = %.2f MB\n",
474 size, (double)size / 1.024e3,
475 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100476#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100477}
478
479#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
480static void genimg_print_time (time_t timestamp)
481{
482#ifndef USE_HOSTCC
483 struct rtc_time tm;
484
485 to_tm (timestamp, &tm);
486 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
487 tm.tm_year, tm.tm_mon, tm.tm_mday,
488 tm.tm_hour, tm.tm_min, tm.tm_sec);
489#else
490 printf ("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100491#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100492}
493#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
494
495/**
496 * get_table_entry_name - translate entry id to long name
497 * @table: pointer to a translation table for entries of a specific type
498 * @msg: message to be returned when translation fails
499 * @id: entry id to be translated
500 *
501 * get_table_entry_name() will go over translation table trying to find
502 * entry that matches given id. If matching entry is found, its long
503 * name is returned to the caller.
504 *
505 * returns:
506 * long entry name if translation succeeds
507 * msg otherwise
508 */
Prafulla Wadaskar4454c9e2009-09-07 14:59:08 +0530509char *get_table_entry_name (table_entry_t *table, char *msg, int id)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100510{
511 for (; table->id >= 0; ++table) {
512 if (table->id == id)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500513#if defined(USE_HOSTCC) || defined(CONFIG_RELOC_FIXUP_WORKS)
Scott Wood68498ab2009-04-02 16:15:10 -0500514 return table->lname;
515#else
516 return table->lname + gd->reloc_off;
517#endif
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100518 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100519 return (msg);
520}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100521
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100522const char *genimg_get_os_name (uint8_t os)
523{
524 return (get_table_entry_name (uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100525}
526
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100527const char *genimg_get_arch_name (uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100528{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100529 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
530}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100531
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100532const char *genimg_get_type_name (uint8_t type)
533{
534 return (get_table_entry_name (uimage_type, "Unknown Image", type));
535}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100536
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100537const char *genimg_get_comp_name (uint8_t comp)
538{
539 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100540}
541
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100542/**
543 * get_table_entry_id - translate short entry name to id
544 * @table: pointer to a translation table for entries of a specific type
545 * @table_name: to be used in case of error
546 * @name: entry short name to be translated
547 *
548 * get_table_entry_id() will go over translation table trying to find
549 * entry that matches given short name. If matching entry is found,
550 * its id returned to the caller.
551 *
552 * returns:
553 * entry id if translation succeeds
554 * -1 otherwise
555 */
Prafulla Wadaskar4454c9e2009-09-07 14:59:08 +0530556int get_table_entry_id (table_entry_t *table,
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100557 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100558{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100559 table_entry_t *t;
560#ifdef USE_HOSTCC
561 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100562
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100563 for (t = table; t->id >= 0; ++t) {
564 if (t->sname && strcasecmp(t->sname, name) == 0)
565 return (t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100566 }
567
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100568 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
569 for (t = table; t->id >= 0; ++t) {
570 if (t->sname == NULL)
571 continue;
572 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
573 first = 0;
574 }
575 fprintf (stderr, "\n");
576#else
577 for (t = table; t->id >= 0; ++t) {
Peter Tyser9057cbf2009-09-21 11:20:36 -0500578#ifdef CONFIG_RELOC_FIXUP_WORKS
579 if (t->sname && strcmp(t->sname, name) == 0)
580#else
Scott Wood68498ab2009-04-02 16:15:10 -0500581 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500582#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100583 return (t->id);
584 }
585 debug ("Invalid %s Type: %s\n", table_name, name);
586#endif /* USE_HOSTCC */
587 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100588}
589
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100590int genimg_get_os_id (const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100591{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100592 return (get_table_entry_id (uimage_os, "OS", name));
593}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100594
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100595int genimg_get_arch_id (const char *name)
596{
597 return (get_table_entry_id (uimage_arch, "CPU", name));
598}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100599
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100600int genimg_get_type_id (const char *name)
601{
602 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100603}
Marian Balakowicz95418502008-01-31 13:55:39 +0100604
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100605int genimg_get_comp_id (const char *name)
606{
607 return (get_table_entry_id (uimage_comp, "Compression", name));
608}
609
610#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100611/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100612 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100613 * @img_addr: image start address
614 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100615 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100616 * legacy or FIT image.
617 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100618 * New uImage format and FDT blob are based on a libfdt. FDT blob
619 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100620 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100621 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100622 * returns:
623 * image format type or IMAGE_FORMAT_INVALID if no image is present
624 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100625int genimg_get_format (void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100626{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200627 ulong format = IMAGE_FORMAT_INVALID;
628 const image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100629#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3b506772009-08-19 11:42:56 +0200630 char *fit_hdr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100631#endif
632
Wolfgang Denk3b506772009-08-19 11:42:56 +0200633 hdr = (const image_header_t *)img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100634 if (image_check_magic(hdr))
635 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100636#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100637 else {
638 fit_hdr = (char *)img_addr;
639 if (fdt_check_header (fit_hdr) == 0)
640 format = IMAGE_FORMAT_FIT;
641 }
642#endif
643
644 return format;
645}
646
647/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100648 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100649 * @img_addr: image start address
650 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100651 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100652 * in a dataflash storage. If so, image is moved to a system RAM memory.
653 *
654 * returns:
655 * image start address after possible relocation from special storage
656 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100657ulong genimg_get_image (ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100658{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100659 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100660
661#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100662 ulong h_size, d_size;
663
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100664 if (addr_dataflash (img_addr)){
Marian Balakowicz535b9102008-02-27 11:00:47 +0100665 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200666 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100667
668 /* get header size */
669 h_size = image_get_header_size ();
670#if defined(CONFIG_FIT)
671 if (sizeof(struct fdt_header) > h_size)
672 h_size = sizeof(struct fdt_header);
673#endif
674
675 /* read in header */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100676 debug (" Reading image header from dataflash address "
677 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100678
Marian Balakowicz535b9102008-02-27 11:00:47 +0100679 read_dataflash (img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100680
Marian Balakowicz535b9102008-02-27 11:00:47 +0100681 /* get data size */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100682 switch (genimg_get_format ((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100683 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3b506772009-08-19 11:42:56 +0200684 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz535b9102008-02-27 11:00:47 +0100685 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
686 ram_addr, d_size);
687 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100688#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100689 case IMAGE_FORMAT_FIT:
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100690 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100691 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
692 ram_addr, d_size);
693 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100694#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100695 default:
696 printf (" No valid image found at 0x%08lx\n", img_addr);
697 return ram_addr;
698 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100699
Marian Balakowicz535b9102008-02-27 11:00:47 +0100700 /* read in image data */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100701 debug (" Reading image remaining data from dataflash address "
702 "%08lx to RAM address %08lx\n", img_addr + h_size,
703 ram_addr + h_size);
704
705 read_dataflash (img_addr + h_size, d_size,
706 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100707
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100708 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100709#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100710
711 return ram_addr;
712}
713
714/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100715 * fit_has_config - check if there is a valid FIT configuration
716 * @images: pointer to the bootm command headers structure
717 *
718 * fit_has_config() checks if there is a FIT configuration in use
719 * (if FTI support is present).
720 *
721 * returns:
722 * 0, no FIT support or no configuration found
723 * 1, configuration found
724 */
725int genimg_has_config (bootm_headers_t *images)
726{
727#if defined(CONFIG_FIT)
728 if (images->fit_uname_cfg)
729 return 1;
730#endif
731 return 0;
732}
733
734/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100735 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100736 * @argc: command argument count
737 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100738 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100739 * @arch: expected ramdisk architecture
740 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
741 * @rd_end: pointer to a ulong variable, will hold ramdisk end
742 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100743 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100744 * Curently supported are the following ramdisk sources:
745 * - multicomponent kernel/ramdisk image,
746 * - commandline provided address of decicated ramdisk image.
747 *
748 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100749 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100750 * rd_start and rd_end are set to ramdisk start/end addresses if
751 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100752 *
Kumar Galab8ca7342008-08-29 19:08:29 -0500753 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz95418502008-01-31 13:55:39 +0100754 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100755 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100756int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
757 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100758{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100759 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100760 ulong rd_data, rd_len;
Wolfgang Denk3b506772009-08-19 11:42:56 +0200761 const image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100762#if defined(CONFIG_FIT)
763 void *fit_hdr;
764 const char *fit_uname_config = NULL;
765 const char *fit_uname_ramdisk = NULL;
766 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100767 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100768 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100769 const void *data;
770 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100771#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100772
Marian Balakowicza50d5152008-03-12 10:12:37 +0100773 *rd_start = 0;
774 *rd_end = 0;
775
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100776 /*
777 * Look for a '-' which indicates to ignore the
778 * ramdisk argument
779 */
780 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
781 debug ("## Skipping init Ramdisk\n");
782 rd_len = rd_data = 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100783 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100784#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100785 if (argc >= 3) {
786 /*
787 * If the init ramdisk comes from the FIT image and
788 * the FIT image address is omitted in the command
789 * line argument, try to use os FIT image address or
790 * default load address.
791 */
792 if (images->fit_uname_os)
793 default_addr = (ulong)images->fit_hdr_os;
794 else
795 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100796
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100797 if (fit_parse_conf (argv[2], default_addr,
798 &rd_addr, &fit_uname_config)) {
799 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
800 fit_uname_config, rd_addr);
801 } else if (fit_parse_subimage (argv[2], default_addr,
802 &rd_addr, &fit_uname_ramdisk)) {
803 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
804 fit_uname_ramdisk, rd_addr);
805 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100806#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100807 {
808 rd_addr = simple_strtoul(argv[2], NULL, 16);
809 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
810 rd_addr);
811 }
812#if defined(CONFIG_FIT)
813 } else {
814 /* use FIT configuration provided in first bootm
815 * command argument
816 */
817 rd_addr = (ulong)images->fit_hdr_os;
818 fit_uname_config = images->fit_uname_cfg;
819 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
820 fit_uname_config, rd_addr);
821
822 /*
823 * Check whether configuration has ramdisk defined,
824 * if not, don't try to use it, quit silently.
825 */
826 fit_hdr = (void *)rd_addr;
827 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
828 if (cfg_noffset < 0) {
829 debug ("* ramdisk: no such config\n");
Michal Simekb2474002008-07-11 10:43:13 +0200830 return 1;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100831 }
832
833 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
834 if (rd_noffset < 0) {
835 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser190ee482008-08-05 10:51:57 -0500836 return 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100837 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100838 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100839#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100840
841 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100842 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100843
844 /*
845 * Check if there is an initrd image at the
846 * address provided in the second bootm argument
847 * check image type, for FIT images get FIT node.
848 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100849 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100850 case IMAGE_FORMAT_LEGACY:
Marian Balakowicza50d5152008-03-12 10:12:37 +0100851 printf ("## Loading init Ramdisk from Legacy "
852 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100853
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100854 show_boot_progress (9);
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100855 rd_hdr = image_get_ramdisk (rd_addr, arch,
856 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100857
Marian Balakowicza50d5152008-03-12 10:12:37 +0100858 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600859 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600860
Marian Balakowicz95418502008-01-31 13:55:39 +0100861 rd_data = image_get_data (rd_hdr);
862 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100863 rd_load = image_get_load (rd_hdr);
864 break;
865#if defined(CONFIG_FIT)
866 case IMAGE_FORMAT_FIT:
867 fit_hdr = (void *)rd_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100868 printf ("## Loading init Ramdisk from FIT "
869 "Image at %08lx ...\n", rd_addr);
870
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100871 show_boot_progress (120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100872 if (!fit_check_format (fit_hdr)) {
873 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100874 show_boot_progress (-120);
Michal Simekb2474002008-07-11 10:43:13 +0200875 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100876 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100877 show_boot_progress (121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100878
879 if (!fit_uname_ramdisk) {
880 /*
881 * no ramdisk image node unit name, try to get config
882 * node first. If config unit node name is NULL
883 * fit_conf_get_node() will try to find default config node
884 */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100885 show_boot_progress (122);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100886 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
887 if (cfg_noffset < 0) {
888 puts ("Could not find configuration node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100889 show_boot_progress (-122);
Michal Simekb2474002008-07-11 10:43:13 +0200890 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100891 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100892 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
893 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100894
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100895 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100896 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
897 } else {
898 /* get ramdisk component image node offset */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100899 show_boot_progress (123);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100900 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
901 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100902 if (rd_noffset < 0) {
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100903 puts ("Could not find subimage node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100904 show_boot_progress (-124);
Michal Simekb2474002008-07-11 10:43:13 +0200905 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100906 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100907
908 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
909
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100910 show_boot_progress (125);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100911 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekb2474002008-07-11 10:43:13 +0200912 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100913
914 /* get ramdisk image data address and length */
915 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
916 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100917 show_boot_progress (-127);
Michal Simekb2474002008-07-11 10:43:13 +0200918 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100919 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100920 show_boot_progress (128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100921
922 rd_data = (ulong)data;
923 rd_len = size;
924
925 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
926 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100927 show_boot_progress (-129);
Michal Simekb2474002008-07-11 10:43:13 +0200928 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100929 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100930 show_boot_progress (129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100931
932 images->fit_hdr_rd = fit_hdr;
933 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100934 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100935 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100936#endif
937 default:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100938 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100939 rd_data = rd_len = rd_load = 0;
Kumar Galab8ca7342008-08-29 19:08:29 -0500940 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100941 }
Marian Balakowicz95418502008-01-31 13:55:39 +0100942
943#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100944 /*
945 * We need to copy the ramdisk to SRAM to let Linux boot
946 */
947 if (rd_data) {
948 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
949 rd_data = rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100950 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100951#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
Marian Balakowicz95418502008-01-31 13:55:39 +0100952
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100953 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200954 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowicz95418502008-01-31 13:55:39 +0100955 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100956 * Now check if we have a legacy mult-component image,
957 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +0100958 */
959 show_boot_progress (13);
960 printf ("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100961 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100962 (ulong)images->legacy_hdr_os);
963
964 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +0100965 } else {
966 /*
967 * no initrd image
968 */
969 show_boot_progress (14);
970 rd_len = rd_data = 0;
971 }
972
973 if (!rd_data) {
974 debug ("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +0100975 } else {
976 *rd_start = rd_data;
977 *rd_end = rd_data + rd_len;
978 }
979 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
980 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600981
982 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +0100983}
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100984
Daniel Hellstrombb9c8292008-03-28 08:29:26 +0100985#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100986/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100987 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -0600988 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100989 * @rd_data: ramdisk data start address
990 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100991 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
992 * start address (after possible relocation)
993 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
994 * end address (after possible relocation)
995 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100996 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100997 * variable and if requested ramdisk data is moved to a specified location.
998 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100999 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1000 * start/end addresses if ramdisk image start and len were provided,
1001 * otherwise set initrd_start and initrd_end set to zeros.
1002 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001003 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001004 * 0 - success
1005 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001006 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001007int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001008 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001009{
1010 char *s;
1011 ulong initrd_high;
1012 int initrd_copy_to_ram = 1;
1013
1014 if ((s = getenv ("initrd_high")) != NULL) {
1015 /* a value of "no" or a similar string will act like 0,
1016 * turning the "load high" feature off. This is intentional.
1017 */
1018 initrd_high = simple_strtoul (s, NULL, 16);
1019 if (initrd_high == ~0)
1020 initrd_copy_to_ram = 0;
1021 } else {
1022 /* not set, no restrictions to load high */
1023 initrd_high = ~0;
1024 }
1025
Marian Balakowicz902ce702008-05-13 15:53:29 +02001026
1027#ifdef CONFIG_LOGBUFFER
1028 /* Prevent initrd from overwriting logbuffer */
1029 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1030#endif
1031
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001032 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1033 initrd_high, initrd_copy_to_ram);
1034
1035 if (rd_data) {
1036 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1037 debug (" in-place initrd\n");
1038 *initrd_start = rd_data;
1039 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001040 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001041 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001042 if (initrd_high)
Becky Bruced26d67c2008-06-09 20:37:18 -05001043 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001044 else
Becky Bruced26d67c2008-06-09 20:37:18 -05001045 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001046
Kumar Galab937bb72008-02-27 21:51:49 -06001047 if (*initrd_start == 0) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001048 puts ("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001049 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001050 }
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001051 show_boot_progress (12);
1052
1053 *initrd_end = *initrd_start + rd_len;
1054 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1055 *initrd_start, *initrd_end);
1056
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001057 memmove_wd ((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001058 (void *)rd_data, rd_len, CHUNKSZ);
1059
1060 puts ("OK\n");
1061 }
1062 } else {
1063 *initrd_start = 0;
1064 *initrd_end = 0;
1065 }
1066 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1067 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001068
Kumar Galab937bb72008-02-27 21:51:49 -06001069 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001070
Kumar Galab937bb72008-02-27 21:51:49 -06001071error:
1072 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001073}
Jean-Christophe PLAGNIOL-VILLARD0ba0a772008-11-24 13:33:51 +01001074#endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001075
Kumar Galaa5130ad2008-08-15 08:24:38 -05001076#ifdef CONFIG_OF_LIBFDT
1077static void fdt_error (const char *msg)
1078{
1079 puts ("ERROR: ");
1080 puts (msg);
1081 puts (" - must RESET the board to recover.\n");
1082}
1083
Wolfgang Denk3b506772009-08-19 11:42:56 +02001084static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001085{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001086 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001087
1088 image_print_contents (fdt_hdr);
1089
1090 puts (" Verifying Checksum ... ");
1091 if (!image_check_hcrc (fdt_hdr)) {
1092 fdt_error ("fdt header checksum invalid");
1093 return NULL;
1094 }
1095
1096 if (!image_check_dcrc (fdt_hdr)) {
1097 fdt_error ("fdt checksum invalid");
1098 return NULL;
1099 }
1100 puts ("OK\n");
1101
1102 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1103 fdt_error ("uImage is not a fdt");
1104 return NULL;
1105 }
1106 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1107 fdt_error ("uImage is compressed");
1108 return NULL;
1109 }
1110 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1111 fdt_error ("uImage data is not a fdt");
1112 return NULL;
1113 }
1114 return fdt_hdr;
1115}
1116
1117/**
1118 * fit_check_fdt - verify FIT format FDT subimage
1119 * @fit_hdr: pointer to the FIT header
1120 * fdt_noffset: FDT subimage node offset within FIT image
1121 * @verify: data CRC verification flag
1122 *
1123 * fit_check_fdt() verifies integrity of the FDT subimage and from
1124 * specified FIT image.
1125 *
1126 * returns:
1127 * 1, on success
1128 * 0, on failure
1129 */
1130#if defined(CONFIG_FIT)
1131static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1132{
1133 fit_image_print (fit, fdt_noffset, " ");
1134
1135 if (verify) {
1136 puts (" Verifying Hash Integrity ... ");
1137 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1138 fdt_error ("Bad Data Hash");
1139 return 0;
1140 }
1141 puts ("OK\n");
1142 }
1143
1144 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1145 fdt_error ("Not a FDT image");
1146 return 0;
1147 }
1148
1149 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1150 fdt_error ("FDT image is compressed");
1151 return 0;
1152 }
1153
1154 return 1;
1155}
1156#endif /* CONFIG_FIT */
1157
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001158#ifndef CONFIG_SYS_FDT_PAD
1159#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Galaa5130ad2008-08-15 08:24:38 -05001160#endif
1161
1162/**
1163 * boot_relocate_fdt - relocate flat device tree
1164 * @lmb: pointer to lmb handle, will be used for memory mgmt
1165 * @bootmap_base: base address of the bootmap region
1166 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1167 * @of_size: pointer to a ulong variable, will hold fdt length
1168 *
1169 * boot_relocate_fdt() determines if the of_flat_tree address is within
1170 * the bootmap and if not relocates it into that region
1171 *
1172 * of_flat_tree and of_size are set to final (after relocation) values
1173 *
1174 * returns:
1175 * 0 - success
1176 * 1 - failure
1177 */
1178int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1179 char **of_flat_tree, ulong *of_size)
1180{
1181 char *fdt_blob = *of_flat_tree;
1182 ulong relocate = 0;
1183 ulong of_len = 0;
1184
1185 /* nothing to do */
1186 if (*of_size == 0)
1187 return 0;
1188
1189 if (fdt_check_header (fdt_blob) != 0) {
1190 fdt_error ("image is not a fdt");
1191 goto error;
1192 }
1193
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001194#ifndef CONFIG_SYS_NO_FLASH
Kumar Galaa5130ad2008-08-15 08:24:38 -05001195 /* move the blob if it is in flash (set relocate) */
1196 if (addr2info ((ulong)fdt_blob) != NULL)
1197 relocate = 1;
1198#endif
1199
1200 /*
1201 * The blob needs to be inside the boot mapping.
1202 */
1203 if (fdt_blob < (char *)bootmap_base)
1204 relocate = 1;
1205
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001206 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1207 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
Kumar Galaa5130ad2008-08-15 08:24:38 -05001208 relocate = 1;
1209
1210 /* move flattend device tree if needed */
1211 if (relocate) {
1212 int err;
1213 ulong of_start = 0;
1214
1215 /* position on a 4K boundary before the alloc_current */
1216 /* Pad the FDT by a specified amount */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001217 of_len = *of_size + CONFIG_SYS_FDT_PAD;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001218 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001219 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
Kumar Galaa5130ad2008-08-15 08:24:38 -05001220
1221 if (of_start == 0) {
1222 puts("device tree - allocation error\n");
1223 goto error;
1224 }
1225
1226 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1227 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1228 of_len, of_len);
1229
1230 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1231 of_start, of_start + of_len - 1);
1232
1233 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1234 if (err != 0) {
1235 fdt_error ("fdt move failed");
1236 goto error;
1237 }
1238 puts ("OK\n");
1239
1240 *of_flat_tree = (char *)of_start;
1241 *of_size = of_len;
1242 } else {
1243 *of_flat_tree = fdt_blob;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001244 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001245 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1246 fdt_set_totalsize(*of_flat_tree, of_len);
1247
1248 *of_size = of_len;
1249 }
1250
Kumar Gala9f4e7e42008-08-15 08:24:39 -05001251 set_working_fdt_addr(*of_flat_tree);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001252 return 0;
1253
1254error:
1255 return 1;
1256}
1257
1258/**
1259 * boot_get_fdt - main fdt handling routine
1260 * @argc: command argument count
1261 * @argv: command argument list
1262 * @images: pointer to the bootm images structure
1263 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1264 * @of_size: pointer to a ulong variable, will hold fdt length
1265 *
1266 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1267 * Curently supported are the following ramdisk sources:
1268 * - multicomponent kernel/ramdisk image,
1269 * - commandline provided address of decicated ramdisk image.
1270 *
1271 * returns:
1272 * 0, if fdt image was found and valid, or skipped
1273 * of_flat_tree and of_size are set to fdt start address and length if
1274 * fdt image is found and valid
1275 *
1276 * 1, if fdt image is found but corrupted
1277 * of_flat_tree and of_size are set to 0 if no fdt exists
1278 */
1279int boot_get_fdt (int flag, int argc, char *argv[], bootm_headers_t *images,
1280 char **of_flat_tree, ulong *of_size)
1281{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001282 const image_header_t *fdt_hdr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001283 ulong fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001284 char *fdt_blob = NULL;
1285 ulong image_start, image_end;
1286 ulong load_start, load_end;
1287#if defined(CONFIG_FIT)
1288 void *fit_hdr;
1289 const char *fit_uname_config = NULL;
1290 const char *fit_uname_fdt = NULL;
1291 ulong default_addr;
1292 int cfg_noffset;
1293 int fdt_noffset;
1294 const void *data;
1295 size_t size;
1296#endif
1297
1298 *of_flat_tree = NULL;
1299 *of_size = 0;
1300
1301 if (argc > 3 || genimg_has_config (images)) {
1302#if defined(CONFIG_FIT)
1303 if (argc > 3) {
1304 /*
1305 * If the FDT blob comes from the FIT image and the
1306 * FIT image address is omitted in the command line
1307 * argument, try to use ramdisk or os FIT image
1308 * address or default load address.
1309 */
1310 if (images->fit_uname_rd)
1311 default_addr = (ulong)images->fit_hdr_rd;
1312 else if (images->fit_uname_os)
1313 default_addr = (ulong)images->fit_hdr_os;
1314 else
1315 default_addr = load_addr;
1316
1317 if (fit_parse_conf (argv[3], default_addr,
1318 &fdt_addr, &fit_uname_config)) {
1319 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1320 fit_uname_config, fdt_addr);
1321 } else if (fit_parse_subimage (argv[3], default_addr,
1322 &fdt_addr, &fit_uname_fdt)) {
1323 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1324 fit_uname_fdt, fdt_addr);
1325 } else
1326#endif
1327 {
1328 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1329 debug ("* fdt: cmdline image address = 0x%08lx\n",
1330 fdt_addr);
1331 }
1332#if defined(CONFIG_FIT)
1333 } else {
1334 /* use FIT configuration provided in first bootm
1335 * command argument
1336 */
1337 fdt_addr = (ulong)images->fit_hdr_os;
1338 fit_uname_config = images->fit_uname_cfg;
1339 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1340 fit_uname_config, fdt_addr);
1341
1342 /*
1343 * Check whether configuration has FDT blob defined,
1344 * if not quit silently.
1345 */
1346 fit_hdr = (void *)fdt_addr;
1347 cfg_noffset = fit_conf_get_node (fit_hdr,
1348 fit_uname_config);
1349 if (cfg_noffset < 0) {
1350 debug ("* fdt: no such config\n");
1351 return 0;
1352 }
1353
1354 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1355 cfg_noffset);
1356 if (fdt_noffset < 0) {
1357 debug ("* fdt: no fdt in config\n");
1358 return 0;
1359 }
1360 }
1361#endif
1362
1363 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1364 fdt_addr);
1365
1366 /* copy from dataflash if needed */
1367 fdt_addr = genimg_get_image (fdt_addr);
1368
1369 /*
1370 * Check if there is an FDT image at the
1371 * address provided in the second bootm argument
1372 * check image type, for FIT images get a FIT node.
1373 */
1374 switch (genimg_get_format ((void *)fdt_addr)) {
1375 case IMAGE_FORMAT_LEGACY:
1376 /* verify fdt_addr points to a valid image header */
1377 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1378 fdt_addr);
1379 fdt_hdr = image_get_fdt (fdt_addr);
1380 if (!fdt_hdr)
1381 goto error;
1382
1383 /*
1384 * move image data to the load address,
1385 * make sure we don't overwrite initial image
1386 */
1387 image_start = (ulong)fdt_hdr;
1388 image_end = image_get_image_end (fdt_hdr);
1389
1390 load_start = image_get_load (fdt_hdr);
1391 load_end = load_start + image_get_data_size (fdt_hdr);
1392
1393 if ((load_start < image_end) && (load_end > image_start)) {
1394 fdt_error ("fdt overwritten");
1395 goto error;
1396 }
1397
1398 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1399 image_get_data (fdt_hdr), load_start);
1400
1401 memmove ((void *)load_start,
1402 (void *)image_get_data (fdt_hdr),
1403 image_get_data_size (fdt_hdr));
1404
1405 fdt_blob = (char *)load_start;
1406 break;
1407 case IMAGE_FORMAT_FIT:
1408 /*
1409 * This case will catch both: new uImage format
1410 * (libfdt based) and raw FDT blob (also libfdt
1411 * based).
1412 */
1413#if defined(CONFIG_FIT)
1414 /* check FDT blob vs FIT blob */
1415 if (fit_check_format ((const void *)fdt_addr)) {
1416 /*
1417 * FIT image
1418 */
1419 fit_hdr = (void *)fdt_addr;
1420 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1421 fdt_addr);
1422
1423 if (!fit_uname_fdt) {
1424 /*
1425 * no FDT blob image node unit name,
1426 * try to get config node first. If
1427 * config unit node name is NULL
1428 * fit_conf_get_node() will try to
1429 * find default config node
1430 */
1431 cfg_noffset = fit_conf_get_node (fit_hdr,
1432 fit_uname_config);
1433
1434 if (cfg_noffset < 0) {
1435 fdt_error ("Could not find configuration node\n");
1436 goto error;
1437 }
1438
1439 fit_uname_config = fdt_get_name (fit_hdr,
1440 cfg_noffset, NULL);
1441 printf (" Using '%s' configuration\n",
1442 fit_uname_config);
1443
1444 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1445 cfg_noffset);
1446 fit_uname_fdt = fit_get_name (fit_hdr,
1447 fdt_noffset, NULL);
1448 } else {
1449 /* get FDT component image node offset */
1450 fdt_noffset = fit_image_get_node (fit_hdr,
1451 fit_uname_fdt);
1452 }
1453 if (fdt_noffset < 0) {
1454 fdt_error ("Could not find subimage node\n");
1455 goto error;
1456 }
1457
1458 printf (" Trying '%s' FDT blob subimage\n",
1459 fit_uname_fdt);
1460
1461 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1462 images->verify))
1463 goto error;
1464
1465 /* get ramdisk image data address and length */
1466 if (fit_image_get_data (fit_hdr, fdt_noffset,
1467 &data, &size)) {
1468 fdt_error ("Could not find FDT subimage data");
1469 goto error;
1470 }
1471
1472 /* verift that image data is a proper FDT blob */
1473 if (fdt_check_header ((char *)data) != 0) {
1474 fdt_error ("Subimage data is not a FTD");
1475 goto error;
1476 }
1477
1478 /*
1479 * move image data to the load address,
1480 * make sure we don't overwrite initial image
1481 */
1482 image_start = (ulong)fit_hdr;
1483 image_end = fit_get_end (fit_hdr);
1484
1485 if (fit_image_get_load (fit_hdr, fdt_noffset,
1486 &load_start) == 0) {
1487 load_end = load_start + size;
1488
1489 if ((load_start < image_end) &&
1490 (load_end > image_start)) {
1491 fdt_error ("FDT overwritten");
1492 goto error;
1493 }
1494
1495 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1496 (ulong)data, load_start);
1497
1498 memmove ((void *)load_start,
1499 (void *)data, size);
1500
1501 fdt_blob = (char *)load_start;
1502 } else {
1503 fdt_blob = (char *)data;
1504 }
1505
1506 images->fit_hdr_fdt = fit_hdr;
1507 images->fit_uname_fdt = fit_uname_fdt;
1508 images->fit_noffset_fdt = fdt_noffset;
1509 break;
1510 } else
1511#endif
1512 {
1513 /*
1514 * FDT blob
1515 */
1516 fdt_blob = (char *)fdt_addr;
1517 debug ("* fdt: raw FDT blob\n");
1518 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1519 }
1520 break;
1521 default:
Anatolij Gustschin128de002008-08-29 21:04:45 +02001522 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001523 goto error;
1524 }
1525
1526 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1527
1528 } else if (images->legacy_hdr_valid &&
1529 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1530
1531 ulong fdt_data, fdt_len;
1532
1533 /*
1534 * Now check if we have a legacy multi-component image,
1535 * get second entry data start address and len.
1536 */
1537 printf ("## Flattened Device Tree from multi "
1538 "component Image at %08lX\n",
1539 (ulong)images->legacy_hdr_os);
1540
1541 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1542 if (fdt_len) {
1543
1544 fdt_blob = (char *)fdt_data;
1545 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1546
1547 if (fdt_check_header (fdt_blob) != 0) {
1548 fdt_error ("image is not a fdt");
1549 goto error;
1550 }
1551
1552 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1553 fdt_error ("fdt size != image size");
1554 goto error;
1555 }
1556 } else {
1557 debug ("## No Flattened Device Tree\n");
1558 return 0;
1559 }
1560 } else {
1561 debug ("## No Flattened Device Tree\n");
1562 return 0;
1563 }
1564
1565 *of_flat_tree = fdt_blob;
1566 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1567 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner7ddfafc2008-08-21 07:12:26 -07001568 (ulong)*of_flat_tree, *of_size);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001569
1570 return 0;
1571
1572error:
1573 *of_flat_tree = 0;
1574 *of_size = 0;
1575 return 1;
1576}
1577#endif /* CONFIG_OF_LIBFDT */
1578
Jean-Christophe PLAGNIOL-VILLARD0ba0a772008-11-24 13:33:51 +01001579#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001580/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001581 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001582 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001583 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1584 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galab937bb72008-02-27 21:51:49 -06001585 * @bootmap_base: ulong variable, holds offset in physical memory to
1586 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001587 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001588 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galab937bb72008-02-27 21:51:49 -06001589 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001590 * variable is present its contents is copied to allocated kernel
1591 * command line.
1592 *
1593 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001594 * 0 - success
1595 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001596 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001597int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galab937bb72008-02-27 21:51:49 -06001598 ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001599{
1600 char *cmdline;
1601 char *s;
1602
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001603 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1604 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -06001605
1606 if (cmdline == NULL)
1607 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001608
1609 if ((s = getenv("bootargs")) == NULL)
1610 s = "";
1611
1612 strcpy(cmdline, s);
1613
1614 *cmd_start = (ulong) & cmdline[0];
1615 *cmd_end = *cmd_start + strlen(cmdline);
1616
1617 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1618
Kumar Galab937bb72008-02-27 21:51:49 -06001619 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001620}
1621
1622/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001623 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001624 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001625 * @kbd: double pointer to board info data
Kumar Galab937bb72008-02-27 21:51:49 -06001626 * @bootmap_base: ulong variable, holds offset in physical memory to
1627 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001628 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001629 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galab937bb72008-02-27 21:51:49 -06001630 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1631 * the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001632 *
1633 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001634 * 0 - success
1635 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001636 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001637int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001638{
Becky Bruced26d67c2008-06-09 20:37:18 -05001639 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001640 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -06001641 if (*kbd == NULL)
1642 return -1;
1643
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001644 **kbd = *(gd->bd);
1645
1646 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1647
1648#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1649 do_bdinfo(NULL, 0, 0, NULL);
1650#endif
1651
Kumar Galab937bb72008-02-27 21:51:49 -06001652 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001653}
1654#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001655#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001656
Marian Balakowicz3413e222008-02-21 17:20:20 +01001657#if defined(CONFIG_FIT)
1658/*****************************************************************************/
1659/* New uImage format routines */
1660/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001661#ifndef USE_HOSTCC
Marian Balakowicz3413e222008-02-21 17:20:20 +01001662static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1663 ulong *addr, const char **name)
1664{
1665 const char *sep;
1666
1667 *addr = addr_curr;
1668 *name = NULL;
1669
1670 sep = strchr (spec, sepc);
1671 if (sep) {
1672 if (sep - spec > 0)
1673 *addr = simple_strtoul (spec, NULL, 16);
1674
1675 *name = sep + 1;
1676 return 1;
1677 }
1678
1679 return 0;
1680}
1681
1682/**
1683 * fit_parse_conf - parse FIT configuration spec
1684 * @spec: input string, containing configuration spec
1685 * @add_curr: current image address (to be used as a possible default)
1686 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1687 * configuration
1688 * @conf_name double pointer to a char, will hold pointer to a configuration
1689 * unit name
1690 *
1691 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1692 * where <addr> is a FIT image address that contains configuration
1693 * with a <conf> unit name.
1694 *
1695 * Address part is optional, and if omitted default add_curr will
1696 * be used instead.
1697 *
1698 * returns:
1699 * 1 if spec is a valid configuration string,
1700 * addr and conf_name are set accordingly
1701 * 0 otherwise
1702 */
1703inline int fit_parse_conf (const char *spec, ulong addr_curr,
1704 ulong *addr, const char **conf_name)
1705{
1706 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1707}
1708
1709/**
1710 * fit_parse_subimage - parse FIT subimage spec
1711 * @spec: input string, containing subimage spec
1712 * @add_curr: current image address (to be used as a possible default)
1713 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1714 * subimage
1715 * @image_name: double pointer to a char, will hold pointer to a subimage name
1716 *
1717 * fit_parse_subimage() expects subimage spec in the for of
1718 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1719 * subimage with a <subimg> unit name.
1720 *
1721 * Address part is optional, and if omitted default add_curr will
1722 * be used instead.
1723 *
1724 * returns:
1725 * 1 if spec is a valid subimage string,
1726 * addr and image_name are set accordingly
1727 * 0 otherwise
1728 */
1729inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1730 ulong *addr, const char **image_name)
1731{
1732 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1733}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001734#endif /* !USE_HOSTCC */
1735
1736static void fit_get_debug (const void *fit, int noffset,
1737 char *prop_name, int err)
1738{
1739 debug ("Can't get '%s' property from FIT 0x%08lx, "
1740 "node: offset %d, name %s (%s)\n",
1741 prop_name, (ulong)fit, noffset,
1742 fit_get_name (fit, noffset, NULL),
1743 fdt_strerror (err));
1744}
1745
1746/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001747 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001748 * @fit: pointer to the FIT format image header
1749 * @p: pointer to prefix string
1750 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001751 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001752 * The routine prints out FIT image properties (root node level) follwed by
1753 * the details of each component image.
1754 *
1755 * returns:
1756 * no returned results
1757 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001758void fit_print_contents (const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001759{
1760 char *desc;
1761 char *uname;
1762 int images_noffset;
1763 int confs_noffset;
1764 int noffset;
1765 int ndepth;
1766 int count = 0;
1767 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001768 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001769#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1770 time_t timestamp;
1771#endif
1772
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001773#ifdef USE_HOSTCC
1774 p = "";
1775#else
1776 p = " ";
1777#endif
1778
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001779 /* Root node properties */
1780 ret = fit_get_desc (fit, 0, &desc);
1781 printf ("%sFIT description: ", p);
1782 if (ret)
1783 printf ("unavailable\n");
1784 else
1785 printf ("%s\n", desc);
1786
1787#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1788 ret = fit_get_timestamp (fit, 0, &timestamp);
1789 printf ("%sCreated: ", p);
1790 if (ret)
1791 printf ("unavailable\n");
1792 else
1793 genimg_print_time (timestamp);
1794#endif
1795
1796 /* Find images parent node offset */
1797 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1798 if (images_noffset < 0) {
1799 printf ("Can't find images parent node '%s' (%s)\n",
1800 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1801 return;
1802 }
1803
1804 /* Process its subnodes, print out component images details */
1805 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1806 (noffset >= 0) && (ndepth > 0);
1807 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1808 if (ndepth == 1) {
1809 /*
1810 * Direct child node of the images parent node,
1811 * i.e. component image node.
1812 */
1813 printf ("%s Image %u (%s)\n", p, count++,
1814 fit_get_name(fit, noffset, NULL));
1815
1816 fit_image_print (fit, noffset, p);
1817 }
1818 }
1819
1820 /* Find configurations parent node offset */
1821 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1822 if (confs_noffset < 0) {
1823 debug ("Can't get configurations parent node '%s' (%s)\n",
1824 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1825 return;
1826 }
1827
1828 /* get default configuration unit name from default property */
1829 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1830 if (uname)
1831 printf ("%s Default Configuration: '%s'\n", p, uname);
1832
1833 /* Process its subnodes, print out configurations details */
1834 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1835 (noffset >= 0) && (ndepth > 0);
1836 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1837 if (ndepth == 1) {
1838 /*
1839 * Direct child node of the configurations parent node,
1840 * i.e. configuration node.
1841 */
1842 printf ("%s Configuration %u (%s)\n", p, count++,
1843 fit_get_name(fit, noffset, NULL));
1844
1845 fit_conf_print (fit, noffset, p);
1846 }
1847 }
1848}
1849
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001850/**
1851 * fit_image_print - prints out the FIT component image details
1852 * @fit: pointer to the FIT format image header
1853 * @image_noffset: offset of the component image node
1854 * @p: pointer to prefix string
1855 *
1856 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001857 * image. If present, hash nodes are printed out as well. Load
1858 * address for images of type firmware is also printed out. Since the load
1859 * address is not mandatory for firmware images, it will be output as
1860 * "unavailable" when not present.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001861 *
1862 * returns:
1863 * no returned results
1864 */
1865void fit_image_print (const void *fit, int image_noffset, const char *p)
1866{
1867 char *desc;
1868 uint8_t type, arch, os, comp;
1869 size_t size;
1870 ulong load, entry;
1871 const void *data;
1872 int noffset;
1873 int ndepth;
1874 int ret;
1875
1876 /* Mandatory properties */
1877 ret = fit_get_desc (fit, image_noffset, &desc);
1878 printf ("%s Description: ", p);
1879 if (ret)
1880 printf ("unavailable\n");
1881 else
1882 printf ("%s\n", desc);
1883
1884 fit_image_get_type (fit, image_noffset, &type);
1885 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1886
1887 fit_image_get_comp (fit, image_noffset, &comp);
1888 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1889
1890 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1891
1892#ifndef USE_HOSTCC
1893 printf ("%s Data Start: ", p);
1894 if (ret)
1895 printf ("unavailable\n");
1896 else
1897 printf ("0x%08lx\n", (ulong)data);
1898#endif
1899
1900 printf ("%s Data Size: ", p);
1901 if (ret)
1902 printf ("unavailable\n");
1903 else
1904 genimg_print_size (size);
1905
1906 /* Remaining, type dependent properties */
1907 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1908 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1909 (type == IH_TYPE_FLATDT)) {
1910 fit_image_get_arch (fit, image_noffset, &arch);
1911 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1912 }
1913
1914 if (type == IH_TYPE_KERNEL) {
1915 fit_image_get_os (fit, image_noffset, &os);
1916 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1917 }
1918
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001919 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1920 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001921 ret = fit_image_get_load (fit, image_noffset, &load);
1922 printf ("%s Load Address: ", p);
1923 if (ret)
1924 printf ("unavailable\n");
1925 else
1926 printf ("0x%08lx\n", load);
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001927 }
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001928
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001929 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001930 fit_image_get_entry (fit, image_noffset, &entry);
1931 printf ("%s Entry Point: ", p);
1932 if (ret)
1933 printf ("unavailable\n");
1934 else
1935 printf ("0x%08lx\n", entry);
1936 }
1937
1938 /* Process all hash subnodes of the component image node */
1939 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1940 (noffset >= 0) && (ndepth > 0);
1941 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1942 if (ndepth == 1) {
1943 /* Direct child node of the component image node */
1944 fit_image_print_hash (fit, noffset, p);
1945 }
1946 }
1947}
1948
1949/**
1950 * fit_image_print_hash - prints out the hash node details
1951 * @fit: pointer to the FIT format image header
1952 * @noffset: offset of the hash node
1953 * @p: pointer to prefix string
1954 *
1955 * fit_image_print_hash() lists properies for the processed hash node
1956 *
1957 * returns:
1958 * no returned results
1959 */
1960void fit_image_print_hash (const void *fit, int noffset, const char *p)
1961{
1962 char *algo;
1963 uint8_t *value;
1964 int value_len;
1965 int i, ret;
1966
1967 /*
1968 * Check subnode name, must be equal to "hash".
1969 * Multiple hash nodes require unique unit node
1970 * names, e.g. hash@1, hash@2, etc.
1971 */
1972 if (strncmp (fit_get_name(fit, noffset, NULL),
1973 FIT_HASH_NODENAME,
1974 strlen(FIT_HASH_NODENAME)) != 0)
1975 return;
1976
1977 debug ("%s Hash node: '%s'\n", p,
1978 fit_get_name (fit, noffset, NULL));
1979
1980 printf ("%s Hash algo: ", p);
1981 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1982 printf ("invalid/unsupported\n");
1983 return;
1984 }
1985 printf ("%s\n", algo);
1986
1987 ret = fit_image_hash_get_value (fit, noffset, &value,
1988 &value_len);
1989 printf ("%s Hash value: ", p);
1990 if (ret) {
1991 printf ("unavailable\n");
1992 } else {
1993 for (i = 0; i < value_len; i++)
1994 printf ("%02x", value[i]);
1995 printf ("\n");
1996 }
1997
1998 debug ("%s Hash len: %d\n", p, value_len);
1999}
2000
2001/**
2002 * fit_get_desc - get node description property
2003 * @fit: pointer to the FIT format image header
2004 * @noffset: node offset
2005 * @desc: double pointer to the char, will hold pointer to the descrption
2006 *
2007 * fit_get_desc() reads description property from a given node, if
2008 * description is found pointer to it is returened in third call argument.
2009 *
2010 * returns:
2011 * 0, on success
2012 * -1, on failure
2013 */
2014int fit_get_desc (const void *fit, int noffset, char **desc)
2015{
2016 int len;
2017
2018 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2019 if (*desc == NULL) {
2020 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2021 return -1;
2022 }
2023
2024 return 0;
2025}
2026
2027/**
2028 * fit_get_timestamp - get node timestamp property
2029 * @fit: pointer to the FIT format image header
2030 * @noffset: node offset
2031 * @timestamp: pointer to the time_t, will hold read timestamp
2032 *
2033 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2034 * is found and has a correct size its value is retured in third call
2035 * argument.
2036 *
2037 * returns:
2038 * 0, on success
2039 * -1, on property read failure
2040 * -2, on wrong timestamp size
2041 */
2042int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2043{
2044 int len;
2045 const void *data;
2046
2047 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2048 if (data == NULL) {
2049 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2050 return -1;
2051 }
2052 if (len != sizeof (uint32_t)) {
2053 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2054 return -2;
2055 }
2056
2057 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2058 return 0;
2059}
2060
2061/**
2062 * fit_image_get_node - get node offset for component image of a given unit name
2063 * @fit: pointer to the FIT format image header
2064 * @image_uname: component image node unit name
2065 *
2066 * fit_image_get_node() finds a component image (withing the '/images'
2067 * node) of a provided unit name. If image is found its node offset is
2068 * returned to the caller.
2069 *
2070 * returns:
2071 * image node offset when found (>=0)
2072 * negative number on failure (FDT_ERR_* code)
2073 */
2074int fit_image_get_node (const void *fit, const char *image_uname)
2075{
2076 int noffset, images_noffset;
2077
2078 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2079 if (images_noffset < 0) {
2080 debug ("Can't find images parent node '%s' (%s)\n",
2081 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2082 return images_noffset;
2083 }
2084
2085 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2086 if (noffset < 0) {
2087 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2088 image_uname, fdt_strerror (noffset));
2089 }
2090
2091 return noffset;
2092}
2093
2094/**
2095 * fit_image_get_os - get os id for a given component image node
2096 * @fit: pointer to the FIT format image header
2097 * @noffset: component image node offset
2098 * @os: pointer to the uint8_t, will hold os numeric id
2099 *
2100 * fit_image_get_os() finds os property in a given component image node.
2101 * If the property is found, its (string) value is translated to the numeric
2102 * id which is returned to the caller.
2103 *
2104 * returns:
2105 * 0, on success
2106 * -1, on failure
2107 */
2108int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2109{
2110 int len;
2111 const void *data;
2112
2113 /* Get OS name from property data */
2114 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2115 if (data == NULL) {
2116 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2117 *os = -1;
2118 return -1;
2119 }
2120
2121 /* Translate OS name to id */
2122 *os = genimg_get_os_id (data);
2123 return 0;
2124}
2125
2126/**
2127 * fit_image_get_arch - get arch id for a given component image node
2128 * @fit: pointer to the FIT format image header
2129 * @noffset: component image node offset
2130 * @arch: pointer to the uint8_t, will hold arch numeric id
2131 *
2132 * fit_image_get_arch() finds arch property in a given component image node.
2133 * If the property is found, its (string) value is translated to the numeric
2134 * id which is returned to the caller.
2135 *
2136 * returns:
2137 * 0, on success
2138 * -1, on failure
2139 */
2140int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2141{
2142 int len;
2143 const void *data;
2144
2145 /* Get architecture name from property data */
2146 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2147 if (data == NULL) {
2148 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2149 *arch = -1;
2150 return -1;
2151 }
2152
2153 /* Translate architecture name to id */
2154 *arch = genimg_get_arch_id (data);
2155 return 0;
2156}
2157
2158/**
2159 * fit_image_get_type - get type id for a given component image node
2160 * @fit: pointer to the FIT format image header
2161 * @noffset: component image node offset
2162 * @type: pointer to the uint8_t, will hold type numeric id
2163 *
2164 * fit_image_get_type() finds type property in a given component image node.
2165 * If the property is found, its (string) value is translated to the numeric
2166 * id which is returned to the caller.
2167 *
2168 * returns:
2169 * 0, on success
2170 * -1, on failure
2171 */
2172int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2173{
2174 int len;
2175 const void *data;
2176
2177 /* Get image type name from property data */
2178 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2179 if (data == NULL) {
2180 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2181 *type = -1;
2182 return -1;
2183 }
2184
2185 /* Translate image type name to id */
2186 *type = genimg_get_type_id (data);
2187 return 0;
2188}
2189
2190/**
2191 * fit_image_get_comp - get comp id for a given component image node
2192 * @fit: pointer to the FIT format image header
2193 * @noffset: component image node offset
2194 * @comp: pointer to the uint8_t, will hold comp numeric id
2195 *
2196 * fit_image_get_comp() finds comp property in a given component image node.
2197 * If the property is found, its (string) value is translated to the numeric
2198 * id which is returned to the caller.
2199 *
2200 * returns:
2201 * 0, on success
2202 * -1, on failure
2203 */
2204int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2205{
2206 int len;
2207 const void *data;
2208
2209 /* Get compression name from property data */
2210 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2211 if (data == NULL) {
2212 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2213 *comp = -1;
2214 return -1;
2215 }
2216
2217 /* Translate compression name to id */
2218 *comp = genimg_get_comp_id (data);
2219 return 0;
2220}
2221
2222/**
2223 * fit_image_get_load - get load address property for a given component image node
2224 * @fit: pointer to the FIT format image header
2225 * @noffset: component image node offset
2226 * @load: pointer to the uint32_t, will hold load address
2227 *
2228 * fit_image_get_load() finds load address property in a given component image node.
2229 * If the property is found, its value is returned to the caller.
2230 *
2231 * returns:
2232 * 0, on success
2233 * -1, on failure
2234 */
2235int fit_image_get_load (const void *fit, int noffset, ulong *load)
2236{
2237 int len;
2238 const uint32_t *data;
2239
2240 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2241 if (data == NULL) {
2242 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2243 return -1;
2244 }
2245
2246 *load = uimage_to_cpu (*data);
2247 return 0;
2248}
2249
2250/**
2251 * fit_image_get_entry - get entry point address property for a given component image node
2252 * @fit: pointer to the FIT format image header
2253 * @noffset: component image node offset
2254 * @entry: pointer to the uint32_t, will hold entry point address
2255 *
2256 * fit_image_get_entry() finds entry point address property in a given component image node.
2257 * If the property is found, its value is returned to the caller.
2258 *
2259 * returns:
2260 * 0, on success
2261 * -1, on failure
2262 */
2263int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2264{
2265 int len;
2266 const uint32_t *data;
2267
2268 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2269 if (data == NULL) {
2270 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2271 return -1;
2272 }
2273
2274 *entry = uimage_to_cpu (*data);
2275 return 0;
2276}
2277
2278/**
2279 * fit_image_get_data - get data property and its size for a given component image node
2280 * @fit: pointer to the FIT format image header
2281 * @noffset: component image node offset
2282 * @data: double pointer to void, will hold data property's data address
2283 * @size: pointer to size_t, will hold data property's data size
2284 *
2285 * fit_image_get_data() finds data property in a given component image node.
2286 * If the property is found its data start address and size are returned to
2287 * the caller.
2288 *
2289 * returns:
2290 * 0, on success
2291 * -1, on failure
2292 */
2293int fit_image_get_data (const void *fit, int noffset,
2294 const void **data, size_t *size)
2295{
2296 int len;
2297
2298 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2299 if (*data == NULL) {
2300 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2301 *size = 0;
2302 return -1;
2303 }
2304
2305 *size = len;
2306 return 0;
2307}
2308
2309/**
2310 * fit_image_hash_get_algo - get hash algorithm name
2311 * @fit: pointer to the FIT format image header
2312 * @noffset: hash node offset
2313 * @algo: double pointer to char, will hold pointer to the algorithm name
2314 *
2315 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2316 * If the property is found its data start address is returned to the caller.
2317 *
2318 * returns:
2319 * 0, on success
2320 * -1, on failure
2321 */
2322int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2323{
2324 int len;
2325
2326 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2327 if (*algo == NULL) {
2328 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2329 return -1;
2330 }
2331
2332 return 0;
2333}
2334
2335/**
2336 * fit_image_hash_get_value - get hash value and length
2337 * @fit: pointer to the FIT format image header
2338 * @noffset: hash node offset
2339 * @value: double pointer to uint8_t, will hold address of a hash value data
2340 * @value_len: pointer to an int, will hold hash data length
2341 *
2342 * fit_image_hash_get_value() finds hash value property in a given hash node.
2343 * If the property is found its data start address and size are returned to
2344 * the caller.
2345 *
2346 * returns:
2347 * 0, on success
2348 * -1, on failure
2349 */
2350int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2351 int *value_len)
2352{
2353 int len;
2354
2355 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2356 if (*value == NULL) {
2357 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2358 *value_len = 0;
2359 return -1;
2360 }
2361
2362 *value_len = len;
2363 return 0;
2364}
2365
2366/**
2367 * fit_set_timestamp - set node timestamp property
2368 * @fit: pointer to the FIT format image header
2369 * @noffset: node offset
2370 * @timestamp: timestamp value to be set
2371 *
2372 * fit_set_timestamp() attempts to set timestamp property in the requested
2373 * node and returns operation status to the caller.
2374 *
2375 * returns:
2376 * 0, on success
2377 * -1, on property read failure
2378 */
2379int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2380{
2381 uint32_t t;
2382 int ret;
2383
2384 t = cpu_to_uimage (timestamp);
2385 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2386 sizeof (uint32_t));
2387 if (ret) {
2388 printf ("Can't set '%s' property for '%s' node (%s)\n",
2389 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2390 fdt_strerror (ret));
2391 return -1;
2392 }
2393
2394 return 0;
2395}
2396
2397/**
2398 * calculate_hash - calculate and return hash for provided input data
2399 * @data: pointer to the input data
2400 * @data_len: data length
2401 * @algo: requested hash algorithm
2402 * @value: pointer to the char, will hold hash value data (caller must
2403 * allocate enough free space)
2404 * value_len: length of the calculated hash
2405 *
2406 * calculate_hash() computes input data hash according to the requested algorithm.
2407 * Resulting hash value is placed in caller provided 'value' buffer, length
2408 * of the calculated hash is returned via value_len pointer argument.
2409 *
2410 * returns:
2411 * 0, on success
2412 * -1, when algo is unsupported
2413 */
2414static int calculate_hash (const void *data, int data_len, const char *algo,
2415 uint8_t *value, int *value_len)
2416{
2417 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002418 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2419 CHUNKSZ_CRC32);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002420 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2421 *value_len = 4;
2422 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002423 sha1_csum_wd ((unsigned char *) data, data_len,
2424 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002425 *value_len = 20;
2426 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002427 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01002428 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002429 } else {
2430 debug ("Unsupported hash alogrithm\n");
2431 return -1;
2432 }
2433 return 0;
2434}
2435
2436#ifdef USE_HOSTCC
2437/**
2438 * fit_set_hashes - process FIT component image nodes and calculate hashes
2439 * @fit: pointer to the FIT format image header
2440 *
2441 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2442 * Hashes are calculated for all component images which have hash subnodes
2443 * with algorithm property set to one of the supported hash algorithms.
2444 *
2445 * returns
2446 * 0, on success
2447 * libfdt error code, on failure
2448 */
2449int fit_set_hashes (void *fit)
2450{
2451 int images_noffset;
2452 int noffset;
2453 int ndepth;
2454 int ret;
2455
2456 /* Find images parent node offset */
2457 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2458 if (images_noffset < 0) {
2459 printf ("Can't find images parent node '%s' (%s)\n",
2460 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2461 return images_noffset;
2462 }
2463
2464 /* Process its subnodes, print out component images details */
2465 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2466 (noffset >= 0) && (ndepth > 0);
2467 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2468 if (ndepth == 1) {
2469 /*
2470 * Direct child node of the images parent node,
2471 * i.e. component image node.
2472 */
2473 ret = fit_image_set_hashes (fit, noffset);
2474 if (ret)
2475 return ret;
2476 }
2477 }
2478
2479 return 0;
2480}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01002481
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002482/**
2483 * fit_image_set_hashes - calculate/set hashes for given component image node
2484 * @fit: pointer to the FIT format image header
2485 * @image_noffset: requested component image node
2486 *
2487 * fit_image_set_hashes() adds hash values for an component image node. All
2488 * existing hash subnodes are checked, if algorithm property is set to one of
2489 * the supported hash algorithms, hash value is computed and corresponding
2490 * hash node property is set, for example:
2491 *
2492 * Input component image node structure:
2493 *
2494 * o image@1 (at image_noffset)
2495 * | - data = [binary data]
2496 * o hash@1
2497 * |- algo = "sha1"
2498 *
2499 * Output component image node structure:
2500 *
2501 * o image@1 (at image_noffset)
2502 * | - data = [binary data]
2503 * o hash@1
2504 * |- algo = "sha1"
2505 * |- value = sha1(data)
2506 *
2507 * returns:
2508 * 0 on sucess
2509 * <0 on failure
2510 */
2511int fit_image_set_hashes (void *fit, int image_noffset)
2512{
2513 const void *data;
2514 size_t size;
2515 char *algo;
2516 uint8_t value[FIT_MAX_HASH_LEN];
2517 int value_len;
2518 int noffset;
2519 int ndepth;
2520
2521 /* Get image data and data length */
2522 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2523 printf ("Can't get image data/size\n");
2524 return -1;
2525 }
2526
2527 /* Process all hash subnodes of the component image node */
2528 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2529 (noffset >= 0) && (ndepth > 0);
2530 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2531 if (ndepth == 1) {
2532 /* Direct child node of the component image node */
2533
2534 /*
2535 * Check subnode name, must be equal to "hash".
2536 * Multiple hash nodes require unique unit node
2537 * names, e.g. hash@1, hash@2, etc.
2538 */
2539 if (strncmp (fit_get_name(fit, noffset, NULL),
2540 FIT_HASH_NODENAME,
2541 strlen(FIT_HASH_NODENAME)) != 0) {
2542 /* Not a hash subnode, skip it */
2543 continue;
2544 }
2545
2546 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2547 printf ("Can't get hash algo property for "
2548 "'%s' hash node in '%s' image node\n",
2549 fit_get_name (fit, noffset, NULL),
2550 fit_get_name (fit, image_noffset, NULL));
2551 return -1;
2552 }
2553
2554 if (calculate_hash (data, size, algo, value, &value_len)) {
2555 printf ("Unsupported hash algorithm (%s) for "
2556 "'%s' hash node in '%s' image node\n",
2557 algo, fit_get_name (fit, noffset, NULL),
2558 fit_get_name (fit, image_noffset, NULL));
2559 return -1;
2560 }
2561
2562 if (fit_image_hash_set_value (fit, noffset, value,
2563 value_len)) {
2564 printf ("Can't set hash value for "
2565 "'%s' hash node in '%s' image node\n",
2566 fit_get_name (fit, noffset, NULL),
2567 fit_get_name (fit, image_noffset, NULL));
2568 return -1;
2569 }
2570 }
2571 }
2572
2573 return 0;
2574}
2575
2576/**
2577 * fit_image_hash_set_value - set hash value in requested has node
2578 * @fit: pointer to the FIT format image header
2579 * @noffset: hash node offset
2580 * @value: hash value to be set
2581 * @value_len: hash value length
2582 *
2583 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2584 * given and returns operation status to the caller.
2585 *
2586 * returns
2587 * 0, on success
2588 * -1, on failure
2589 */
2590int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2591 int value_len)
2592{
2593 int ret;
2594
2595 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2596 if (ret) {
2597 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2598 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2599 fdt_strerror (ret));
2600 return -1;
2601 }
2602
2603 return 0;
2604}
2605#endif /* USE_HOSTCC */
2606
2607/**
2608 * fit_image_check_hashes - verify data intergity
2609 * @fit: pointer to the FIT format image header
2610 * @image_noffset: component image node offset
2611 *
2612 * fit_image_check_hashes() goes over component image hash nodes,
2613 * re-calculates each data hash and compares with the value stored in hash
2614 * node.
2615 *
2616 * returns:
2617 * 1, if all hashes are valid
2618 * 0, otherwise (or on error)
2619 */
2620int fit_image_check_hashes (const void *fit, int image_noffset)
2621{
2622 const void *data;
2623 size_t size;
2624 char *algo;
2625 uint8_t *fit_value;
2626 int fit_value_len;
2627 uint8_t value[FIT_MAX_HASH_LEN];
2628 int value_len;
2629 int noffset;
2630 int ndepth;
2631 char *err_msg = "";
2632
2633 /* Get image data and data length */
2634 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2635 printf ("Can't get image data/size\n");
2636 return 0;
2637 }
2638
2639 /* Process all hash subnodes of the component image node */
2640 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2641 (noffset >= 0) && (ndepth > 0);
2642 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2643 if (ndepth == 1) {
2644 /* Direct child node of the component image node */
2645
2646 /*
2647 * Check subnode name, must be equal to "hash".
2648 * Multiple hash nodes require unique unit node
2649 * names, e.g. hash@1, hash@2, etc.
2650 */
2651 if (strncmp (fit_get_name(fit, noffset, NULL),
2652 FIT_HASH_NODENAME,
2653 strlen(FIT_HASH_NODENAME)) != 0)
2654 continue;
2655
2656 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002657 err_msg = " error!\nCan't get hash algo "
2658 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002659 goto error;
2660 }
2661 printf ("%s", algo);
2662
2663 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2664 &fit_value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002665 err_msg = " error!\nCan't get hash value "
2666 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002667 goto error;
2668 }
2669
2670 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002671 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002672 goto error;
2673 }
2674
2675 if (value_len != fit_value_len) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002676 err_msg = " error !\nBad hash value len";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002677 goto error;
2678 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002679 err_msg = " error!\nBad hash value";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002680 goto error;
2681 }
2682 printf ("+ ");
2683 }
2684 }
2685
2686 return 1;
2687
2688error:
2689 printf ("%s for '%s' hash node in '%s' image node\n",
2690 err_msg, fit_get_name (fit, noffset, NULL),
2691 fit_get_name (fit, image_noffset, NULL));
2692 return 0;
2693}
2694
2695/**
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002696 * fit_all_image_check_hashes - verify data intergity for all images
2697 * @fit: pointer to the FIT format image header
2698 *
2699 * fit_all_image_check_hashes() goes over all images in the FIT and
2700 * for every images checks if all it's hashes are valid.
2701 *
2702 * returns:
2703 * 1, if all hashes of all images are valid
2704 * 0, otherwise (or on error)
2705 */
2706int fit_all_image_check_hashes (const void *fit)
2707{
2708 int images_noffset;
2709 int noffset;
2710 int ndepth;
2711 int count;
2712
2713 /* Find images parent node offset */
2714 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2715 if (images_noffset < 0) {
2716 printf ("Can't find images parent node '%s' (%s)\n",
2717 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2718 return 0;
2719 }
2720
2721 /* Process all image subnodes, check hashes for each */
2722 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2723 (ulong)fit);
2724 for (ndepth = 0, count = 0,
2725 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2726 (noffset >= 0) && (ndepth > 0);
2727 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2728 if (ndepth == 1) {
2729 /*
2730 * Direct child node of the images parent node,
2731 * i.e. component image node.
2732 */
2733 printf (" Hash(es) for Image %u (%s): ", count++,
2734 fit_get_name (fit, noffset, NULL));
2735
2736 if (!fit_image_check_hashes (fit, noffset))
2737 return 0;
2738 printf ("\n");
2739 }
2740 }
2741 return 1;
2742}
2743
2744/**
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002745 * fit_image_check_os - check whether image node is of a given os type
2746 * @fit: pointer to the FIT format image header
2747 * @noffset: component image node offset
2748 * @os: requested image os
2749 *
2750 * fit_image_check_os() reads image os property and compares its numeric
2751 * id with the requested os. Comparison result is returned to the caller.
2752 *
2753 * returns:
2754 * 1 if image is of given os type
2755 * 0 otherwise (or on error)
2756 */
2757int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2758{
2759 uint8_t image_os;
2760
2761 if (fit_image_get_os (fit, noffset, &image_os))
2762 return 0;
2763 return (os == image_os);
2764}
2765
2766/**
2767 * fit_image_check_arch - check whether image node is of a given arch
2768 * @fit: pointer to the FIT format image header
2769 * @noffset: component image node offset
2770 * @arch: requested imagearch
2771 *
2772 * fit_image_check_arch() reads image arch property and compares its numeric
2773 * id with the requested arch. Comparison result is returned to the caller.
2774 *
2775 * returns:
2776 * 1 if image is of given arch
2777 * 0 otherwise (or on error)
2778 */
2779int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2780{
2781 uint8_t image_arch;
2782
2783 if (fit_image_get_arch (fit, noffset, &image_arch))
2784 return 0;
2785 return (arch == image_arch);
2786}
2787
2788/**
2789 * fit_image_check_type - check whether image node is of a given type
2790 * @fit: pointer to the FIT format image header
2791 * @noffset: component image node offset
2792 * @type: requested image type
2793 *
2794 * fit_image_check_type() reads image type property and compares its numeric
2795 * id with the requested type. Comparison result is returned to the caller.
2796 *
2797 * returns:
2798 * 1 if image is of given type
2799 * 0 otherwise (or on error)
2800 */
2801int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2802{
2803 uint8_t image_type;
2804
2805 if (fit_image_get_type (fit, noffset, &image_type))
2806 return 0;
2807 return (type == image_type);
2808}
2809
2810/**
2811 * fit_image_check_comp - check whether image node uses given compression
2812 * @fit: pointer to the FIT format image header
2813 * @noffset: component image node offset
2814 * @comp: requested image compression type
2815 *
2816 * fit_image_check_comp() reads image compression property and compares its
2817 * numeric id with the requested compression type. Comparison result is
2818 * returned to the caller.
2819 *
2820 * returns:
2821 * 1 if image uses requested compression
2822 * 0 otherwise (or on error)
2823 */
2824int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2825{
2826 uint8_t image_comp;
2827
2828 if (fit_image_get_comp (fit, noffset, &image_comp))
2829 return 0;
2830 return (comp == image_comp);
2831}
2832
2833/**
2834 * fit_check_format - sanity check FIT image format
2835 * @fit: pointer to the FIT format image header
2836 *
2837 * fit_check_format() runs a basic sanity FIT image verification.
2838 * Routine checks for mandatory properties, nodes, etc.
2839 *
2840 * returns:
2841 * 1, on success
2842 * 0, on failure
2843 */
2844int fit_check_format (const void *fit)
2845{
2846 /* mandatory / node 'description' property */
2847 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2848 debug ("Wrong FIT format: no description\n");
2849 return 0;
2850 }
2851
2852#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2853 /* mandatory / node 'timestamp' property */
2854 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002855 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002856 return 0;
2857 }
2858#endif
2859
2860 /* mandatory subimages parent '/images' node */
2861 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2862 debug ("Wrong FIT format: no images parent node\n");
2863 return 0;
2864 }
2865
2866 return 1;
2867}
2868
2869/**
2870 * fit_conf_get_node - get node offset for configuration of a given unit name
2871 * @fit: pointer to the FIT format image header
2872 * @conf_uname: configuration node unit name
2873 *
2874 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2875 * parant node) of a provided unit name. If configuration is found its node offset
2876 * is returned to the caller.
2877 *
2878 * When NULL is provided in second argument fit_conf_get_node() will search
2879 * for a default configuration node instead. Default configuration node unit name
2880 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2881 *
2882 * returns:
2883 * configuration node offset when found (>=0)
2884 * negative number on failure (FDT_ERR_* code)
2885 */
2886int fit_conf_get_node (const void *fit, const char *conf_uname)
2887{
2888 int noffset, confs_noffset;
2889 int len;
2890
2891 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2892 if (confs_noffset < 0) {
2893 debug ("Can't find configurations parent node '%s' (%s)\n",
2894 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2895 return confs_noffset;
2896 }
2897
2898 if (conf_uname == NULL) {
2899 /* get configuration unit name from the default property */
2900 debug ("No configuration specified, trying default...\n");
2901 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2902 if (conf_uname == NULL) {
2903 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2904 return len;
2905 }
2906 debug ("Found default configuration: '%s'\n", conf_uname);
2907 }
2908
2909 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2910 if (noffset < 0) {
2911 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2912 conf_uname, fdt_strerror (noffset));
2913 }
2914
2915 return noffset;
2916}
2917
2918static int __fit_conf_get_prop_node (const void *fit, int noffset,
2919 const char *prop_name)
2920{
2921 char *uname;
2922 int len;
2923
2924 /* get kernel image unit name from configuration kernel property */
2925 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2926 if (uname == NULL)
2927 return len;
2928
2929 return fit_image_get_node (fit, uname);
2930}
2931
2932/**
2933 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2934 * a given configuration
2935 * @fit: pointer to the FIT format image header
2936 * @noffset: configuration node offset
2937 *
2938 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2939 * configuration FIT_KERNEL_PROP property and translates it to the node
2940 * offset.
2941 *
2942 * returns:
2943 * image node offset when found (>=0)
2944 * negative number on failure (FDT_ERR_* code)
2945 */
2946int fit_conf_get_kernel_node (const void *fit, int noffset)
2947{
2948 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2949}
2950
2951/**
2952 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2953 * a given configuration
2954 * @fit: pointer to the FIT format image header
2955 * @noffset: configuration node offset
2956 *
2957 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2958 * configuration FIT_KERNEL_PROP property and translates it to the node
2959 * offset.
2960 *
2961 * returns:
2962 * image node offset when found (>=0)
2963 * negative number on failure (FDT_ERR_* code)
2964 */
2965int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2966{
2967 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2968}
2969
2970/**
2971 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2972 * a given configuration
2973 * @fit: pointer to the FIT format image header
2974 * @noffset: configuration node offset
2975 *
2976 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2977 * configuration FIT_KERNEL_PROP property and translates it to the node
2978 * offset.
2979 *
2980 * returns:
2981 * image node offset when found (>=0)
2982 * negative number on failure (FDT_ERR_* code)
2983 */
2984int fit_conf_get_fdt_node (const void *fit, int noffset)
2985{
2986 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2987}
2988
2989/**
2990 * fit_conf_print - prints out the FIT configuration details
2991 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01002992 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002993 * @p: pointer to prefix string
2994 *
2995 * fit_conf_print() lists all mandatory properies for the processed
2996 * configuration node.
2997 *
2998 * returns:
2999 * no returned results
3000 */
3001void fit_conf_print (const void *fit, int noffset, const char *p)
3002{
3003 char *desc;
3004 char *uname;
3005 int ret;
3006
3007 /* Mandatory properties */
3008 ret = fit_get_desc (fit, noffset, &desc);
3009 printf ("%s Description: ", p);
3010 if (ret)
3011 printf ("unavailable\n");
3012 else
3013 printf ("%s\n", desc);
3014
3015 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3016 printf ("%s Kernel: ", p);
3017 if (uname == NULL)
3018 printf ("unavailable\n");
3019 else
3020 printf ("%s\n", uname);
3021
3022 /* Optional properties */
3023 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3024 if (uname)
3025 printf ("%s Init Ramdisk: %s\n", p, uname);
3026
3027 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3028 if (uname)
3029 printf ("%s FDT: %s\n", p, uname);
3030}
Marian Balakowicza50d5152008-03-12 10:12:37 +01003031
3032/**
3033 * fit_check_ramdisk - verify FIT format ramdisk subimage
3034 * @fit_hdr: pointer to the FIT ramdisk header
3035 * @rd_noffset: ramdisk subimage node offset within FIT image
3036 * @arch: requested ramdisk image architecture type
3037 * @verify: data CRC verification flag
3038 *
3039 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3040 * specified FIT image.
3041 *
3042 * returns:
3043 * 1, on success
3044 * 0, on failure
3045 */
3046#ifndef USE_HOSTCC
3047static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3048{
3049 fit_image_print (fit, rd_noffset, " ");
3050
3051 if (verify) {
3052 puts (" Verifying Hash Integrity ... ");
3053 if (!fit_image_check_hashes (fit, rd_noffset)) {
3054 puts ("Bad Data Hash\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003055 show_boot_progress (-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003056 return 0;
3057 }
3058 puts ("OK\n");
3059 }
3060
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003061 show_boot_progress (126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003062 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3063 !fit_image_check_arch (fit, rd_noffset, arch) ||
3064 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3065 printf ("No Linux %s Ramdisk Image\n",
3066 genimg_get_arch_name(arch));
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003067 show_boot_progress (-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003068 return 0;
3069 }
3070
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003071 show_boot_progress (127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003072 return 1;
3073}
3074#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01003075#endif /* CONFIG_FIT */