blob: d0f169d660cce7472a54967b837169ed6d0aa07a [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", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100151 { -1, "", "", },
152};
153
Mike Frysingera2c67e92008-03-31 11:02:01 -0400154uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200155uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100156#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
157static void genimg_print_time (time_t timestamp);
158#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100159
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100160/*****************************************************************************/
161/* Legacy format routines */
162/*****************************************************************************/
Wolfgang Denk3b506772009-08-19 11:42:56 +0200163int image_check_hcrc (const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100164{
165 ulong hcrc;
166 ulong len = image_get_header_size ();
167 image_header_t header;
168
169 /* Copy header so we can blank CRC field for re-calculation */
170 memmove (&header, (char *)hdr, image_get_header_size ());
171 image_set_hcrc (&header, 0);
172
173 hcrc = crc32 (0, (unsigned char *)&header, len);
174
175 return (hcrc == image_get_hcrc (hdr));
176}
177
Wolfgang Denk3b506772009-08-19 11:42:56 +0200178int image_check_dcrc (const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100179{
180 ulong data = image_get_data (hdr);
181 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200182 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100183
184 return (dcrc == image_get_dcrc (hdr));
185}
186
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100187/**
188 * image_multi_count - get component (sub-image) count
189 * @hdr: pointer to the header of the multi component image
190 *
191 * image_multi_count() returns number of components in a multi
192 * component image.
193 *
194 * Note: no checking of the image type is done, caller must pass
195 * a valid multi component image.
196 *
197 * returns:
198 * number of components
199 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200200ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100201{
202 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100203 uint32_t *size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100204
205 /* get start of the image payload, which in case of multi
206 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100207 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100208
209 /* count non empty slots */
210 for (i = 0; size[i]; ++i)
211 count++;
212
213 return count;
214}
215
216/**
217 * image_multi_getimg - get component data address and size
218 * @hdr: pointer to the header of the multi component image
219 * @idx: index of the requested component
220 * @data: pointer to a ulong variable, will hold component data address
221 * @len: pointer to a ulong variable, will hold component size
222 *
223 * image_multi_getimg() returns size and data address for the requested
224 * component in a multi component image.
225 *
226 * Note: no checking of the image type is done, caller must pass
227 * a valid multi component image.
228 *
229 * returns:
230 * data address and size of the component, if idx is valid
231 * 0 in data and len, if idx is out of range
232 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200233void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100234 ulong *data, ulong *len)
235{
236 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100237 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700238 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100239
240 /* get number of component */
241 count = image_multi_count (hdr);
242
243 /* get start of the image payload, which in case of multi
244 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100245 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100246
247 /* get address of the proper component data start, which means
248 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowicza1474212008-02-29 16:00:06 +0100249 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100250
251 if (idx < count) {
Marian Balakowicza1474212008-02-29 16:00:06 +0100252 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100253 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100254
255 /* go over all indices preceding requested component idx */
256 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-05-10 14:02:04 -0700257 /* add up i-th component size, rounding up to 4 bytes */
258 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100259 }
260
261 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700262 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100263 } else {
264 *len = 0;
265 *data = 0;
266 }
267}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100268
Wolfgang Denk3b506772009-08-19 11:42:56 +0200269static void image_print_type (const image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100270{
271 const char *os, *arch, *type, *comp;
272
273 os = genimg_get_os_name (image_get_os (hdr));
274 arch = genimg_get_arch_name (image_get_arch (hdr));
275 type = genimg_get_type_name (image_get_type (hdr));
276 comp = genimg_get_comp_name (image_get_comp (hdr));
277
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100278 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100279}
280
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100281/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200282 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3b506772009-08-19 11:42:56 +0200283 * @ptr: pointer to the legacy format image header
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100284 * @p: pointer to prefix string
285 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200286 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100287 * The routine prints out all header fields followed by the size/offset data
288 * for MULTI/SCRIPT images.
289 *
290 * returns:
291 * no returned results
292 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200293void image_print_contents (const void *ptr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100294{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200295 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200296 const char *p;
297
298#ifdef USE_HOSTCC
299 p = "";
300#else
301 p = " ";
302#endif
303
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100304 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
305#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
306 printf ("%sCreated: ", p);
307 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100308#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100309 printf ("%sImage Type: ", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100310 image_print_type (hdr);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100311 printf ("%sData Size: ", p);
312 genimg_print_size (image_get_data_size (hdr));
313 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
314 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100315
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100316 if (image_check_type (hdr, IH_TYPE_MULTI) ||
317 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100318 int i;
319 ulong data, len;
320 ulong count = image_multi_count (hdr);
321
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100322 printf ("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100323 for (i = 0; i < count; i++) {
324 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100325
326 printf ("%s Image %d: ", p, i);
327 genimg_print_size (len);
328
329 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
330 /*
331 * the user may need to know offsets
332 * if planning to do something with
333 * multiple files
334 */
335 printf ("%s Offset = 0x%08lx\n", p, data);
336 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100337 }
338 }
339}
340
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100341
342#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100343/**
344 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100345 * @rd_addr: ramdisk image start address
346 * @arch: expected ramdisk architecture
347 * @verify: checksum verification flag
348 *
349 * image_get_ramdisk() returns a pointer to the verified ramdisk image
350 * header. Routine receives image start address and expected architecture
351 * flag. Verification done covers data and header integrity and os/type/arch
352 * fields checking.
353 *
354 * If dataflash support is enabled routine checks for dataflash addresses
355 * and handles required dataflash reads.
356 *
357 * returns:
358 * pointer to a ramdisk image header, if image was found and valid
359 * otherwise, return NULL
360 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200361static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100362 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100363{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200364 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100365
366 if (!image_check_magic (rd_hdr)) {
367 puts ("Bad Magic Number\n");
368 show_boot_progress (-10);
369 return NULL;
370 }
371
372 if (!image_check_hcrc (rd_hdr)) {
373 puts ("Bad Header Checksum\n");
374 show_boot_progress (-11);
375 return NULL;
376 }
377
378 show_boot_progress (10);
379 image_print_contents (rd_hdr);
380
381 if (verify) {
382 puts(" Verifying Checksum ... ");
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200383 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100384 puts ("Bad Data CRC\n");
385 show_boot_progress (-12);
386 return NULL;
387 }
388 puts("OK\n");
389 }
390
391 show_boot_progress (11);
392
393 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
394 !image_check_arch (rd_hdr, arch) ||
395 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
396 printf ("No Linux %s Ramdisk Image\n",
397 genimg_get_arch_name(arch));
398 show_boot_progress (-13);
399 return NULL;
400 }
401
402 return rd_hdr;
403}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100404#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100405
406/*****************************************************************************/
407/* Shared dual-format routines */
408/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100409#ifndef USE_HOSTCC
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200410int getenv_yesno (char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100411{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200412 char *s = getenv (var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100413 return (s && (*s == 'n')) ? 0 : 1;
414}
415
416ulong getenv_bootm_low(void)
417{
418 char *s = getenv ("bootm_low");
419 if (s) {
420 ulong tmp = simple_strtoul (s, NULL, 16);
421 return tmp;
422 }
423
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200424#if defined(CONFIG_SYS_SDRAM_BASE)
425 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100426#elif defined(CONFIG_ARM)
427 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100428#else
429 return 0;
430#endif
431}
432
Becky Bruced26d67c2008-06-09 20:37:18 -0500433phys_size_t getenv_bootm_size(void)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100434{
435 char *s = getenv ("bootm_size");
436 if (s) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500437 phys_size_t tmp;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200438#ifdef CONFIG_SYS_64BIT_STRTOUL
Becky Bruced26d67c2008-06-09 20:37:18 -0500439 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
440#else
441 tmp = (phys_size_t)simple_strtoul (s, NULL, 16);
442#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100443 return tmp;
444 }
445
Marian Balakowicz2499b042008-03-12 12:14:15 +0100446#if defined(CONFIG_ARM)
447 return gd->bd->bi_dram[0].size;
448#else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100449 return gd->bd->bi_memsize;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100450#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100451}
452
453void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
454{
455#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
456 while (len > 0) {
457 size_t tail = (len > chunksz) ? chunksz : len;
458 WATCHDOG_RESET ();
459 memmove (to, from, tail);
460 to += tail;
461 from += tail;
462 len -= tail;
463 }
464#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
465 memmove (to, from, len);
466#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
467}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100468#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100469
Prafulla Wadaskar49432ab2009-09-07 14:59:07 +0530470void genimg_print_size (uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100471{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100472#ifndef USE_HOSTCC
473 printf ("%d Bytes = ", size);
474 print_size (size, "\n");
475#else
476 printf ("%d Bytes = %.2f kB = %.2f MB\n",
477 size, (double)size / 1.024e3,
478 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100479#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100480}
481
482#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
483static void genimg_print_time (time_t timestamp)
484{
485#ifndef USE_HOSTCC
486 struct rtc_time tm;
487
488 to_tm (timestamp, &tm);
489 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
490 tm.tm_year, tm.tm_mon, tm.tm_mday,
491 tm.tm_hour, tm.tm_min, tm.tm_sec);
492#else
493 printf ("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100494#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100495}
496#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
497
498/**
499 * get_table_entry_name - translate entry id to long name
500 * @table: pointer to a translation table for entries of a specific type
501 * @msg: message to be returned when translation fails
502 * @id: entry id to be translated
503 *
504 * get_table_entry_name() will go over translation table trying to find
505 * entry that matches given id. If matching entry is found, its long
506 * name is returned to the caller.
507 *
508 * returns:
509 * long entry name if translation succeeds
510 * msg otherwise
511 */
Prafulla Wadaskar4454c9e2009-09-07 14:59:08 +0530512char *get_table_entry_name (table_entry_t *table, char *msg, int id)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100513{
514 for (; table->id >= 0; ++table) {
515 if (table->id == id)
Scott Wood68498ab2009-04-02 16:15:10 -0500516#ifdef USE_HOSTCC
517 return table->lname;
518#else
519 return table->lname + gd->reloc_off;
520#endif
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100521 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100522 return (msg);
523}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100524
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100525const char *genimg_get_os_name (uint8_t os)
526{
527 return (get_table_entry_name (uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100528}
529
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100530const char *genimg_get_arch_name (uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100531{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100532 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
533}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100534
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100535const char *genimg_get_type_name (uint8_t type)
536{
537 return (get_table_entry_name (uimage_type, "Unknown Image", type));
538}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100539
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100540const char *genimg_get_comp_name (uint8_t comp)
541{
542 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100543}
544
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100545/**
546 * get_table_entry_id - translate short entry name to id
547 * @table: pointer to a translation table for entries of a specific type
548 * @table_name: to be used in case of error
549 * @name: entry short name to be translated
550 *
551 * get_table_entry_id() will go over translation table trying to find
552 * entry that matches given short name. If matching entry is found,
553 * its id returned to the caller.
554 *
555 * returns:
556 * entry id if translation succeeds
557 * -1 otherwise
558 */
Prafulla Wadaskar4454c9e2009-09-07 14:59:08 +0530559int get_table_entry_id (table_entry_t *table,
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100560 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100561{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100562 table_entry_t *t;
563#ifdef USE_HOSTCC
564 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100565
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100566 for (t = table; t->id >= 0; ++t) {
567 if (t->sname && strcasecmp(t->sname, name) == 0)
568 return (t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100569 }
570
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100571 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
572 for (t = table; t->id >= 0; ++t) {
573 if (t->sname == NULL)
574 continue;
575 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
576 first = 0;
577 }
578 fprintf (stderr, "\n");
579#else
580 for (t = table; t->id >= 0; ++t) {
Scott Wood68498ab2009-04-02 16:15:10 -0500581 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100582 return (t->id);
583 }
584 debug ("Invalid %s Type: %s\n", table_name, name);
585#endif /* USE_HOSTCC */
586 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100587}
588
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100589int genimg_get_os_id (const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100590{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100591 return (get_table_entry_id (uimage_os, "OS", name));
592}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100593
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100594int genimg_get_arch_id (const char *name)
595{
596 return (get_table_entry_id (uimage_arch, "CPU", name));
597}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100598
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100599int genimg_get_type_id (const char *name)
600{
601 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100602}
Marian Balakowicz95418502008-01-31 13:55:39 +0100603
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100604int genimg_get_comp_id (const char *name)
605{
606 return (get_table_entry_id (uimage_comp, "Compression", name));
607}
608
609#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100610/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100611 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100612 * @img_addr: image start address
613 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100614 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100615 * legacy or FIT image.
616 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100617 * New uImage format and FDT blob are based on a libfdt. FDT blob
618 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100619 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100620 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100621 * returns:
622 * image format type or IMAGE_FORMAT_INVALID if no image is present
623 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100624int genimg_get_format (void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100625{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200626 ulong format = IMAGE_FORMAT_INVALID;
627 const image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100628#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3b506772009-08-19 11:42:56 +0200629 char *fit_hdr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100630#endif
631
Wolfgang Denk3b506772009-08-19 11:42:56 +0200632 hdr = (const image_header_t *)img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100633 if (image_check_magic(hdr))
634 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100635#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100636 else {
637 fit_hdr = (char *)img_addr;
638 if (fdt_check_header (fit_hdr) == 0)
639 format = IMAGE_FORMAT_FIT;
640 }
641#endif
642
643 return format;
644}
645
646/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100647 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100648 * @img_addr: image start address
649 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100650 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100651 * in a dataflash storage. If so, image is moved to a system RAM memory.
652 *
653 * returns:
654 * image start address after possible relocation from special storage
655 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100656ulong genimg_get_image (ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100657{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100658 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100659
660#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100661 ulong h_size, d_size;
662
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100663 if (addr_dataflash (img_addr)){
Marian Balakowicz535b9102008-02-27 11:00:47 +0100664 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200665 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100666
667 /* get header size */
668 h_size = image_get_header_size ();
669#if defined(CONFIG_FIT)
670 if (sizeof(struct fdt_header) > h_size)
671 h_size = sizeof(struct fdt_header);
672#endif
673
674 /* read in header */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100675 debug (" Reading image header from dataflash address "
676 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100677
Marian Balakowicz535b9102008-02-27 11:00:47 +0100678 read_dataflash (img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100679
Marian Balakowicz535b9102008-02-27 11:00:47 +0100680 /* get data size */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100681 switch (genimg_get_format ((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100682 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3b506772009-08-19 11:42:56 +0200683 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz535b9102008-02-27 11:00:47 +0100684 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
685 ram_addr, d_size);
686 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100687#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100688 case IMAGE_FORMAT_FIT:
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100689 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100690 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
691 ram_addr, d_size);
692 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100693#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100694 default:
695 printf (" No valid image found at 0x%08lx\n", img_addr);
696 return ram_addr;
697 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100698
Marian Balakowicz535b9102008-02-27 11:00:47 +0100699 /* read in image data */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100700 debug (" Reading image remaining data from dataflash address "
701 "%08lx to RAM address %08lx\n", img_addr + h_size,
702 ram_addr + h_size);
703
704 read_dataflash (img_addr + h_size, d_size,
705 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100706
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100707 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100708#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100709
710 return ram_addr;
711}
712
713/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100714 * fit_has_config - check if there is a valid FIT configuration
715 * @images: pointer to the bootm command headers structure
716 *
717 * fit_has_config() checks if there is a FIT configuration in use
718 * (if FTI support is present).
719 *
720 * returns:
721 * 0, no FIT support or no configuration found
722 * 1, configuration found
723 */
724int genimg_has_config (bootm_headers_t *images)
725{
726#if defined(CONFIG_FIT)
727 if (images->fit_uname_cfg)
728 return 1;
729#endif
730 return 0;
731}
732
733/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100734 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100735 * @argc: command argument count
736 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100737 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100738 * @arch: expected ramdisk architecture
739 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
740 * @rd_end: pointer to a ulong variable, will hold ramdisk end
741 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100742 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100743 * Curently supported are the following ramdisk sources:
744 * - multicomponent kernel/ramdisk image,
745 * - commandline provided address of decicated ramdisk image.
746 *
747 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100748 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100749 * rd_start and rd_end are set to ramdisk start/end addresses if
750 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100751 *
Kumar Galab8ca7342008-08-29 19:08:29 -0500752 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz95418502008-01-31 13:55:39 +0100753 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100754 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100755int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
756 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100757{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100758 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100759 ulong rd_data, rd_len;
Wolfgang Denk3b506772009-08-19 11:42:56 +0200760 const image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100761#if defined(CONFIG_FIT)
762 void *fit_hdr;
763 const char *fit_uname_config = NULL;
764 const char *fit_uname_ramdisk = NULL;
765 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100766 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100767 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100768 const void *data;
769 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100770#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100771
Marian Balakowicza50d5152008-03-12 10:12:37 +0100772 *rd_start = 0;
773 *rd_end = 0;
774
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100775 /*
776 * Look for a '-' which indicates to ignore the
777 * ramdisk argument
778 */
779 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
780 debug ("## Skipping init Ramdisk\n");
781 rd_len = rd_data = 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100782 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100783#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100784 if (argc >= 3) {
785 /*
786 * If the init ramdisk comes from the FIT image and
787 * the FIT image address is omitted in the command
788 * line argument, try to use os FIT image address or
789 * default load address.
790 */
791 if (images->fit_uname_os)
792 default_addr = (ulong)images->fit_hdr_os;
793 else
794 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100795
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100796 if (fit_parse_conf (argv[2], default_addr,
797 &rd_addr, &fit_uname_config)) {
798 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
799 fit_uname_config, rd_addr);
800 } else if (fit_parse_subimage (argv[2], default_addr,
801 &rd_addr, &fit_uname_ramdisk)) {
802 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
803 fit_uname_ramdisk, rd_addr);
804 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100805#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100806 {
807 rd_addr = simple_strtoul(argv[2], NULL, 16);
808 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
809 rd_addr);
810 }
811#if defined(CONFIG_FIT)
812 } else {
813 /* use FIT configuration provided in first bootm
814 * command argument
815 */
816 rd_addr = (ulong)images->fit_hdr_os;
817 fit_uname_config = images->fit_uname_cfg;
818 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
819 fit_uname_config, rd_addr);
820
821 /*
822 * Check whether configuration has ramdisk defined,
823 * if not, don't try to use it, quit silently.
824 */
825 fit_hdr = (void *)rd_addr;
826 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
827 if (cfg_noffset < 0) {
828 debug ("* ramdisk: no such config\n");
Michal Simekb2474002008-07-11 10:43:13 +0200829 return 1;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100830 }
831
832 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
833 if (rd_noffset < 0) {
834 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser190ee482008-08-05 10:51:57 -0500835 return 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100836 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100837 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100838#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100839
840 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100841 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100842
843 /*
844 * Check if there is an initrd image at the
845 * address provided in the second bootm argument
846 * check image type, for FIT images get FIT node.
847 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100848 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100849 case IMAGE_FORMAT_LEGACY:
Marian Balakowicza50d5152008-03-12 10:12:37 +0100850 printf ("## Loading init Ramdisk from Legacy "
851 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100852
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100853 show_boot_progress (9);
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100854 rd_hdr = image_get_ramdisk (rd_addr, arch,
855 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100856
Marian Balakowicza50d5152008-03-12 10:12:37 +0100857 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600858 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600859
Marian Balakowicz95418502008-01-31 13:55:39 +0100860 rd_data = image_get_data (rd_hdr);
861 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100862 rd_load = image_get_load (rd_hdr);
863 break;
864#if defined(CONFIG_FIT)
865 case IMAGE_FORMAT_FIT:
866 fit_hdr = (void *)rd_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100867 printf ("## Loading init Ramdisk from FIT "
868 "Image at %08lx ...\n", rd_addr);
869
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100870 show_boot_progress (120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100871 if (!fit_check_format (fit_hdr)) {
872 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100873 show_boot_progress (-120);
Michal Simekb2474002008-07-11 10:43:13 +0200874 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100875 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100876 show_boot_progress (121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100877
878 if (!fit_uname_ramdisk) {
879 /*
880 * no ramdisk image node unit name, try to get config
881 * node first. If config unit node name is NULL
882 * fit_conf_get_node() will try to find default config node
883 */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100884 show_boot_progress (122);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100885 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
886 if (cfg_noffset < 0) {
887 puts ("Could not find configuration node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100888 show_boot_progress (-122);
Michal Simekb2474002008-07-11 10:43:13 +0200889 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100890 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100891 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
892 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100893
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100894 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100895 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
896 } else {
897 /* get ramdisk component image node offset */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100898 show_boot_progress (123);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100899 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
900 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100901 if (rd_noffset < 0) {
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100902 puts ("Could not find subimage node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100903 show_boot_progress (-124);
Michal Simekb2474002008-07-11 10:43:13 +0200904 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100905 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100906
907 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
908
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100909 show_boot_progress (125);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100910 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekb2474002008-07-11 10:43:13 +0200911 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100912
913 /* get ramdisk image data address and length */
914 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
915 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100916 show_boot_progress (-127);
Michal Simekb2474002008-07-11 10:43:13 +0200917 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100918 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100919 show_boot_progress (128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100920
921 rd_data = (ulong)data;
922 rd_len = size;
923
924 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
925 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100926 show_boot_progress (-129);
Michal Simekb2474002008-07-11 10:43:13 +0200927 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100928 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100929 show_boot_progress (129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100930
931 images->fit_hdr_rd = fit_hdr;
932 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100933 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100934 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100935#endif
936 default:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100937 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100938 rd_data = rd_len = rd_load = 0;
Kumar Galab8ca7342008-08-29 19:08:29 -0500939 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100940 }
Marian Balakowicz95418502008-01-31 13:55:39 +0100941
942#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100943 /*
944 * We need to copy the ramdisk to SRAM to let Linux boot
945 */
946 if (rd_data) {
947 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
948 rd_data = rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100949 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100950#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
Marian Balakowicz95418502008-01-31 13:55:39 +0100951
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100952 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200953 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowicz95418502008-01-31 13:55:39 +0100954 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100955 * Now check if we have a legacy mult-component image,
956 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +0100957 */
958 show_boot_progress (13);
959 printf ("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100960 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100961 (ulong)images->legacy_hdr_os);
962
963 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +0100964 } else {
965 /*
966 * no initrd image
967 */
968 show_boot_progress (14);
969 rd_len = rd_data = 0;
970 }
971
972 if (!rd_data) {
973 debug ("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +0100974 } else {
975 *rd_start = rd_data;
976 *rd_end = rd_data + rd_len;
977 }
978 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
979 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600980
981 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +0100982}
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100983
Daniel Hellstrombb9c8292008-03-28 08:29:26 +0100984#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100985/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100986 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -0600987 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100988 * @rd_data: ramdisk data start address
989 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100990 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
991 * start address (after possible relocation)
992 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
993 * end address (after possible relocation)
994 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100995 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100996 * variable and if requested ramdisk data is moved to a specified location.
997 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100998 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
999 * start/end addresses if ramdisk image start and len were provided,
1000 * otherwise set initrd_start and initrd_end set to zeros.
1001 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001002 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001003 * 0 - success
1004 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001005 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001006int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001007 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001008{
1009 char *s;
1010 ulong initrd_high;
1011 int initrd_copy_to_ram = 1;
1012
1013 if ((s = getenv ("initrd_high")) != NULL) {
1014 /* a value of "no" or a similar string will act like 0,
1015 * turning the "load high" feature off. This is intentional.
1016 */
1017 initrd_high = simple_strtoul (s, NULL, 16);
1018 if (initrd_high == ~0)
1019 initrd_copy_to_ram = 0;
1020 } else {
1021 /* not set, no restrictions to load high */
1022 initrd_high = ~0;
1023 }
1024
Marian Balakowicz902ce702008-05-13 15:53:29 +02001025
1026#ifdef CONFIG_LOGBUFFER
1027 /* Prevent initrd from overwriting logbuffer */
1028 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1029#endif
1030
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001031 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1032 initrd_high, initrd_copy_to_ram);
1033
1034 if (rd_data) {
1035 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1036 debug (" in-place initrd\n");
1037 *initrd_start = rd_data;
1038 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001039 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001040 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001041 if (initrd_high)
Becky Bruced26d67c2008-06-09 20:37:18 -05001042 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001043 else
Becky Bruced26d67c2008-06-09 20:37:18 -05001044 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001045
Kumar Galab937bb72008-02-27 21:51:49 -06001046 if (*initrd_start == 0) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001047 puts ("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001048 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001049 }
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001050 show_boot_progress (12);
1051
1052 *initrd_end = *initrd_start + rd_len;
1053 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1054 *initrd_start, *initrd_end);
1055
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001056 memmove_wd ((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001057 (void *)rd_data, rd_len, CHUNKSZ);
1058
1059 puts ("OK\n");
1060 }
1061 } else {
1062 *initrd_start = 0;
1063 *initrd_end = 0;
1064 }
1065 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1066 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001067
Kumar Galab937bb72008-02-27 21:51:49 -06001068 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001069
Kumar Galab937bb72008-02-27 21:51:49 -06001070error:
1071 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001072}
Jean-Christophe PLAGNIOL-VILLARD0ba0a772008-11-24 13:33:51 +01001073#endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001074
Kumar Galaa5130ad2008-08-15 08:24:38 -05001075#ifdef CONFIG_OF_LIBFDT
1076static void fdt_error (const char *msg)
1077{
1078 puts ("ERROR: ");
1079 puts (msg);
1080 puts (" - must RESET the board to recover.\n");
1081}
1082
Wolfgang Denk3b506772009-08-19 11:42:56 +02001083static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001084{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001085 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001086
1087 image_print_contents (fdt_hdr);
1088
1089 puts (" Verifying Checksum ... ");
1090 if (!image_check_hcrc (fdt_hdr)) {
1091 fdt_error ("fdt header checksum invalid");
1092 return NULL;
1093 }
1094
1095 if (!image_check_dcrc (fdt_hdr)) {
1096 fdt_error ("fdt checksum invalid");
1097 return NULL;
1098 }
1099 puts ("OK\n");
1100
1101 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1102 fdt_error ("uImage is not a fdt");
1103 return NULL;
1104 }
1105 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1106 fdt_error ("uImage is compressed");
1107 return NULL;
1108 }
1109 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1110 fdt_error ("uImage data is not a fdt");
1111 return NULL;
1112 }
1113 return fdt_hdr;
1114}
1115
1116/**
1117 * fit_check_fdt - verify FIT format FDT subimage
1118 * @fit_hdr: pointer to the FIT header
1119 * fdt_noffset: FDT subimage node offset within FIT image
1120 * @verify: data CRC verification flag
1121 *
1122 * fit_check_fdt() verifies integrity of the FDT subimage and from
1123 * specified FIT image.
1124 *
1125 * returns:
1126 * 1, on success
1127 * 0, on failure
1128 */
1129#if defined(CONFIG_FIT)
1130static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1131{
1132 fit_image_print (fit, fdt_noffset, " ");
1133
1134 if (verify) {
1135 puts (" Verifying Hash Integrity ... ");
1136 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1137 fdt_error ("Bad Data Hash");
1138 return 0;
1139 }
1140 puts ("OK\n");
1141 }
1142
1143 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1144 fdt_error ("Not a FDT image");
1145 return 0;
1146 }
1147
1148 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1149 fdt_error ("FDT image is compressed");
1150 return 0;
1151 }
1152
1153 return 1;
1154}
1155#endif /* CONFIG_FIT */
1156
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001157#ifndef CONFIG_SYS_FDT_PAD
1158#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Galaa5130ad2008-08-15 08:24:38 -05001159#endif
1160
1161/**
1162 * boot_relocate_fdt - relocate flat device tree
1163 * @lmb: pointer to lmb handle, will be used for memory mgmt
1164 * @bootmap_base: base address of the bootmap region
1165 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1166 * @of_size: pointer to a ulong variable, will hold fdt length
1167 *
1168 * boot_relocate_fdt() determines if the of_flat_tree address is within
1169 * the bootmap and if not relocates it into that region
1170 *
1171 * of_flat_tree and of_size are set to final (after relocation) values
1172 *
1173 * returns:
1174 * 0 - success
1175 * 1 - failure
1176 */
1177int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1178 char **of_flat_tree, ulong *of_size)
1179{
1180 char *fdt_blob = *of_flat_tree;
1181 ulong relocate = 0;
1182 ulong of_len = 0;
1183
1184 /* nothing to do */
1185 if (*of_size == 0)
1186 return 0;
1187
1188 if (fdt_check_header (fdt_blob) != 0) {
1189 fdt_error ("image is not a fdt");
1190 goto error;
1191 }
1192
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001193#ifndef CONFIG_SYS_NO_FLASH
Kumar Galaa5130ad2008-08-15 08:24:38 -05001194 /* move the blob if it is in flash (set relocate) */
1195 if (addr2info ((ulong)fdt_blob) != NULL)
1196 relocate = 1;
1197#endif
1198
1199 /*
1200 * The blob needs to be inside the boot mapping.
1201 */
1202 if (fdt_blob < (char *)bootmap_base)
1203 relocate = 1;
1204
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001205 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1206 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
Kumar Galaa5130ad2008-08-15 08:24:38 -05001207 relocate = 1;
1208
1209 /* move flattend device tree if needed */
1210 if (relocate) {
1211 int err;
1212 ulong of_start = 0;
1213
1214 /* position on a 4K boundary before the alloc_current */
1215 /* Pad the FDT by a specified amount */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001216 of_len = *of_size + CONFIG_SYS_FDT_PAD;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001217 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001218 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
Kumar Galaa5130ad2008-08-15 08:24:38 -05001219
1220 if (of_start == 0) {
1221 puts("device tree - allocation error\n");
1222 goto error;
1223 }
1224
1225 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1226 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1227 of_len, of_len);
1228
1229 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1230 of_start, of_start + of_len - 1);
1231
1232 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1233 if (err != 0) {
1234 fdt_error ("fdt move failed");
1235 goto error;
1236 }
1237 puts ("OK\n");
1238
1239 *of_flat_tree = (char *)of_start;
1240 *of_size = of_len;
1241 } else {
1242 *of_flat_tree = fdt_blob;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001243 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001244 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1245 fdt_set_totalsize(*of_flat_tree, of_len);
1246
1247 *of_size = of_len;
1248 }
1249
Kumar Gala9f4e7e42008-08-15 08:24:39 -05001250 set_working_fdt_addr(*of_flat_tree);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001251 return 0;
1252
1253error:
1254 return 1;
1255}
1256
1257/**
1258 * boot_get_fdt - main fdt handling routine
1259 * @argc: command argument count
1260 * @argv: command argument list
1261 * @images: pointer to the bootm images structure
1262 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1263 * @of_size: pointer to a ulong variable, will hold fdt length
1264 *
1265 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1266 * Curently supported are the following ramdisk sources:
1267 * - multicomponent kernel/ramdisk image,
1268 * - commandline provided address of decicated ramdisk image.
1269 *
1270 * returns:
1271 * 0, if fdt image was found and valid, or skipped
1272 * of_flat_tree and of_size are set to fdt start address and length if
1273 * fdt image is found and valid
1274 *
1275 * 1, if fdt image is found but corrupted
1276 * of_flat_tree and of_size are set to 0 if no fdt exists
1277 */
1278int boot_get_fdt (int flag, int argc, char *argv[], bootm_headers_t *images,
1279 char **of_flat_tree, ulong *of_size)
1280{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001281 const image_header_t *fdt_hdr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001282 ulong fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001283 char *fdt_blob = NULL;
1284 ulong image_start, image_end;
1285 ulong load_start, load_end;
1286#if defined(CONFIG_FIT)
1287 void *fit_hdr;
1288 const char *fit_uname_config = NULL;
1289 const char *fit_uname_fdt = NULL;
1290 ulong default_addr;
1291 int cfg_noffset;
1292 int fdt_noffset;
1293 const void *data;
1294 size_t size;
1295#endif
1296
1297 *of_flat_tree = NULL;
1298 *of_size = 0;
1299
1300 if (argc > 3 || genimg_has_config (images)) {
1301#if defined(CONFIG_FIT)
1302 if (argc > 3) {
1303 /*
1304 * If the FDT blob comes from the FIT image and the
1305 * FIT image address is omitted in the command line
1306 * argument, try to use ramdisk or os FIT image
1307 * address or default load address.
1308 */
1309 if (images->fit_uname_rd)
1310 default_addr = (ulong)images->fit_hdr_rd;
1311 else if (images->fit_uname_os)
1312 default_addr = (ulong)images->fit_hdr_os;
1313 else
1314 default_addr = load_addr;
1315
1316 if (fit_parse_conf (argv[3], default_addr,
1317 &fdt_addr, &fit_uname_config)) {
1318 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1319 fit_uname_config, fdt_addr);
1320 } else if (fit_parse_subimage (argv[3], default_addr,
1321 &fdt_addr, &fit_uname_fdt)) {
1322 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1323 fit_uname_fdt, fdt_addr);
1324 } else
1325#endif
1326 {
1327 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1328 debug ("* fdt: cmdline image address = 0x%08lx\n",
1329 fdt_addr);
1330 }
1331#if defined(CONFIG_FIT)
1332 } else {
1333 /* use FIT configuration provided in first bootm
1334 * command argument
1335 */
1336 fdt_addr = (ulong)images->fit_hdr_os;
1337 fit_uname_config = images->fit_uname_cfg;
1338 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1339 fit_uname_config, fdt_addr);
1340
1341 /*
1342 * Check whether configuration has FDT blob defined,
1343 * if not quit silently.
1344 */
1345 fit_hdr = (void *)fdt_addr;
1346 cfg_noffset = fit_conf_get_node (fit_hdr,
1347 fit_uname_config);
1348 if (cfg_noffset < 0) {
1349 debug ("* fdt: no such config\n");
1350 return 0;
1351 }
1352
1353 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1354 cfg_noffset);
1355 if (fdt_noffset < 0) {
1356 debug ("* fdt: no fdt in config\n");
1357 return 0;
1358 }
1359 }
1360#endif
1361
1362 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1363 fdt_addr);
1364
1365 /* copy from dataflash if needed */
1366 fdt_addr = genimg_get_image (fdt_addr);
1367
1368 /*
1369 * Check if there is an FDT image at the
1370 * address provided in the second bootm argument
1371 * check image type, for FIT images get a FIT node.
1372 */
1373 switch (genimg_get_format ((void *)fdt_addr)) {
1374 case IMAGE_FORMAT_LEGACY:
1375 /* verify fdt_addr points to a valid image header */
1376 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1377 fdt_addr);
1378 fdt_hdr = image_get_fdt (fdt_addr);
1379 if (!fdt_hdr)
1380 goto error;
1381
1382 /*
1383 * move image data to the load address,
1384 * make sure we don't overwrite initial image
1385 */
1386 image_start = (ulong)fdt_hdr;
1387 image_end = image_get_image_end (fdt_hdr);
1388
1389 load_start = image_get_load (fdt_hdr);
1390 load_end = load_start + image_get_data_size (fdt_hdr);
1391
1392 if ((load_start < image_end) && (load_end > image_start)) {
1393 fdt_error ("fdt overwritten");
1394 goto error;
1395 }
1396
1397 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1398 image_get_data (fdt_hdr), load_start);
1399
1400 memmove ((void *)load_start,
1401 (void *)image_get_data (fdt_hdr),
1402 image_get_data_size (fdt_hdr));
1403
1404 fdt_blob = (char *)load_start;
1405 break;
1406 case IMAGE_FORMAT_FIT:
1407 /*
1408 * This case will catch both: new uImage format
1409 * (libfdt based) and raw FDT blob (also libfdt
1410 * based).
1411 */
1412#if defined(CONFIG_FIT)
1413 /* check FDT blob vs FIT blob */
1414 if (fit_check_format ((const void *)fdt_addr)) {
1415 /*
1416 * FIT image
1417 */
1418 fit_hdr = (void *)fdt_addr;
1419 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1420 fdt_addr);
1421
1422 if (!fit_uname_fdt) {
1423 /*
1424 * no FDT blob image node unit name,
1425 * try to get config node first. If
1426 * config unit node name is NULL
1427 * fit_conf_get_node() will try to
1428 * find default config node
1429 */
1430 cfg_noffset = fit_conf_get_node (fit_hdr,
1431 fit_uname_config);
1432
1433 if (cfg_noffset < 0) {
1434 fdt_error ("Could not find configuration node\n");
1435 goto error;
1436 }
1437
1438 fit_uname_config = fdt_get_name (fit_hdr,
1439 cfg_noffset, NULL);
1440 printf (" Using '%s' configuration\n",
1441 fit_uname_config);
1442
1443 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1444 cfg_noffset);
1445 fit_uname_fdt = fit_get_name (fit_hdr,
1446 fdt_noffset, NULL);
1447 } else {
1448 /* get FDT component image node offset */
1449 fdt_noffset = fit_image_get_node (fit_hdr,
1450 fit_uname_fdt);
1451 }
1452 if (fdt_noffset < 0) {
1453 fdt_error ("Could not find subimage node\n");
1454 goto error;
1455 }
1456
1457 printf (" Trying '%s' FDT blob subimage\n",
1458 fit_uname_fdt);
1459
1460 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1461 images->verify))
1462 goto error;
1463
1464 /* get ramdisk image data address and length */
1465 if (fit_image_get_data (fit_hdr, fdt_noffset,
1466 &data, &size)) {
1467 fdt_error ("Could not find FDT subimage data");
1468 goto error;
1469 }
1470
1471 /* verift that image data is a proper FDT blob */
1472 if (fdt_check_header ((char *)data) != 0) {
1473 fdt_error ("Subimage data is not a FTD");
1474 goto error;
1475 }
1476
1477 /*
1478 * move image data to the load address,
1479 * make sure we don't overwrite initial image
1480 */
1481 image_start = (ulong)fit_hdr;
1482 image_end = fit_get_end (fit_hdr);
1483
1484 if (fit_image_get_load (fit_hdr, fdt_noffset,
1485 &load_start) == 0) {
1486 load_end = load_start + size;
1487
1488 if ((load_start < image_end) &&
1489 (load_end > image_start)) {
1490 fdt_error ("FDT overwritten");
1491 goto error;
1492 }
1493
1494 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1495 (ulong)data, load_start);
1496
1497 memmove ((void *)load_start,
1498 (void *)data, size);
1499
1500 fdt_blob = (char *)load_start;
1501 } else {
1502 fdt_blob = (char *)data;
1503 }
1504
1505 images->fit_hdr_fdt = fit_hdr;
1506 images->fit_uname_fdt = fit_uname_fdt;
1507 images->fit_noffset_fdt = fdt_noffset;
1508 break;
1509 } else
1510#endif
1511 {
1512 /*
1513 * FDT blob
1514 */
1515 fdt_blob = (char *)fdt_addr;
1516 debug ("* fdt: raw FDT blob\n");
1517 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1518 }
1519 break;
1520 default:
Anatolij Gustschin128de002008-08-29 21:04:45 +02001521 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001522 goto error;
1523 }
1524
1525 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1526
1527 } else if (images->legacy_hdr_valid &&
1528 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1529
1530 ulong fdt_data, fdt_len;
1531
1532 /*
1533 * Now check if we have a legacy multi-component image,
1534 * get second entry data start address and len.
1535 */
1536 printf ("## Flattened Device Tree from multi "
1537 "component Image at %08lX\n",
1538 (ulong)images->legacy_hdr_os);
1539
1540 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1541 if (fdt_len) {
1542
1543 fdt_blob = (char *)fdt_data;
1544 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1545
1546 if (fdt_check_header (fdt_blob) != 0) {
1547 fdt_error ("image is not a fdt");
1548 goto error;
1549 }
1550
1551 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1552 fdt_error ("fdt size != image size");
1553 goto error;
1554 }
1555 } else {
1556 debug ("## No Flattened Device Tree\n");
1557 return 0;
1558 }
1559 } else {
1560 debug ("## No Flattened Device Tree\n");
1561 return 0;
1562 }
1563
1564 *of_flat_tree = fdt_blob;
1565 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1566 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner7ddfafc2008-08-21 07:12:26 -07001567 (ulong)*of_flat_tree, *of_size);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001568
1569 return 0;
1570
1571error:
1572 *of_flat_tree = 0;
1573 *of_size = 0;
1574 return 1;
1575}
1576#endif /* CONFIG_OF_LIBFDT */
1577
Jean-Christophe PLAGNIOL-VILLARD0ba0a772008-11-24 13:33:51 +01001578#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001579/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001580 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001581 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001582 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1583 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galab937bb72008-02-27 21:51:49 -06001584 * @bootmap_base: ulong variable, holds offset in physical memory to
1585 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001586 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001587 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galab937bb72008-02-27 21:51:49 -06001588 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001589 * variable is present its contents is copied to allocated kernel
1590 * command line.
1591 *
1592 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001593 * 0 - success
1594 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001595 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001596int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galab937bb72008-02-27 21:51:49 -06001597 ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001598{
1599 char *cmdline;
1600 char *s;
1601
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001602 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1603 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -06001604
1605 if (cmdline == NULL)
1606 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001607
1608 if ((s = getenv("bootargs")) == NULL)
1609 s = "";
1610
1611 strcpy(cmdline, s);
1612
1613 *cmd_start = (ulong) & cmdline[0];
1614 *cmd_end = *cmd_start + strlen(cmdline);
1615
1616 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1617
Kumar Galab937bb72008-02-27 21:51:49 -06001618 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001619}
1620
1621/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001622 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001623 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001624 * @kbd: double pointer to board info data
Kumar Galab937bb72008-02-27 21:51:49 -06001625 * @bootmap_base: ulong variable, holds offset in physical memory to
1626 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001627 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001628 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galab937bb72008-02-27 21:51:49 -06001629 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1630 * the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001631 *
1632 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001633 * 0 - success
1634 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001635 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001636int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001637{
Becky Bruced26d67c2008-06-09 20:37:18 -05001638 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001639 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -06001640 if (*kbd == NULL)
1641 return -1;
1642
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001643 **kbd = *(gd->bd);
1644
1645 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1646
1647#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1648 do_bdinfo(NULL, 0, 0, NULL);
1649#endif
1650
Kumar Galab937bb72008-02-27 21:51:49 -06001651 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001652}
1653#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001654#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001655
Marian Balakowicz3413e222008-02-21 17:20:20 +01001656#if defined(CONFIG_FIT)
1657/*****************************************************************************/
1658/* New uImage format routines */
1659/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001660#ifndef USE_HOSTCC
Marian Balakowicz3413e222008-02-21 17:20:20 +01001661static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1662 ulong *addr, const char **name)
1663{
1664 const char *sep;
1665
1666 *addr = addr_curr;
1667 *name = NULL;
1668
1669 sep = strchr (spec, sepc);
1670 if (sep) {
1671 if (sep - spec > 0)
1672 *addr = simple_strtoul (spec, NULL, 16);
1673
1674 *name = sep + 1;
1675 return 1;
1676 }
1677
1678 return 0;
1679}
1680
1681/**
1682 * fit_parse_conf - parse FIT configuration spec
1683 * @spec: input string, containing configuration spec
1684 * @add_curr: current image address (to be used as a possible default)
1685 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1686 * configuration
1687 * @conf_name double pointer to a char, will hold pointer to a configuration
1688 * unit name
1689 *
1690 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1691 * where <addr> is a FIT image address that contains configuration
1692 * with a <conf> unit name.
1693 *
1694 * Address part is optional, and if omitted default add_curr will
1695 * be used instead.
1696 *
1697 * returns:
1698 * 1 if spec is a valid configuration string,
1699 * addr and conf_name are set accordingly
1700 * 0 otherwise
1701 */
1702inline int fit_parse_conf (const char *spec, ulong addr_curr,
1703 ulong *addr, const char **conf_name)
1704{
1705 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1706}
1707
1708/**
1709 * fit_parse_subimage - parse FIT subimage spec
1710 * @spec: input string, containing subimage spec
1711 * @add_curr: current image address (to be used as a possible default)
1712 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1713 * subimage
1714 * @image_name: double pointer to a char, will hold pointer to a subimage name
1715 *
1716 * fit_parse_subimage() expects subimage spec in the for of
1717 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1718 * subimage with a <subimg> unit name.
1719 *
1720 * Address part is optional, and if omitted default add_curr will
1721 * be used instead.
1722 *
1723 * returns:
1724 * 1 if spec is a valid subimage string,
1725 * addr and image_name are set accordingly
1726 * 0 otherwise
1727 */
1728inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1729 ulong *addr, const char **image_name)
1730{
1731 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1732}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001733#endif /* !USE_HOSTCC */
1734
1735static void fit_get_debug (const void *fit, int noffset,
1736 char *prop_name, int err)
1737{
1738 debug ("Can't get '%s' property from FIT 0x%08lx, "
1739 "node: offset %d, name %s (%s)\n",
1740 prop_name, (ulong)fit, noffset,
1741 fit_get_name (fit, noffset, NULL),
1742 fdt_strerror (err));
1743}
1744
1745/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001746 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001747 * @fit: pointer to the FIT format image header
1748 * @p: pointer to prefix string
1749 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001750 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001751 * The routine prints out FIT image properties (root node level) follwed by
1752 * the details of each component image.
1753 *
1754 * returns:
1755 * no returned results
1756 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001757void fit_print_contents (const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001758{
1759 char *desc;
1760 char *uname;
1761 int images_noffset;
1762 int confs_noffset;
1763 int noffset;
1764 int ndepth;
1765 int count = 0;
1766 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001767 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001768#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1769 time_t timestamp;
1770#endif
1771
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001772#ifdef USE_HOSTCC
1773 p = "";
1774#else
1775 p = " ";
1776#endif
1777
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001778 /* Root node properties */
1779 ret = fit_get_desc (fit, 0, &desc);
1780 printf ("%sFIT description: ", p);
1781 if (ret)
1782 printf ("unavailable\n");
1783 else
1784 printf ("%s\n", desc);
1785
1786#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1787 ret = fit_get_timestamp (fit, 0, &timestamp);
1788 printf ("%sCreated: ", p);
1789 if (ret)
1790 printf ("unavailable\n");
1791 else
1792 genimg_print_time (timestamp);
1793#endif
1794
1795 /* Find images parent node offset */
1796 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1797 if (images_noffset < 0) {
1798 printf ("Can't find images parent node '%s' (%s)\n",
1799 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1800 return;
1801 }
1802
1803 /* Process its subnodes, print out component images details */
1804 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1805 (noffset >= 0) && (ndepth > 0);
1806 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1807 if (ndepth == 1) {
1808 /*
1809 * Direct child node of the images parent node,
1810 * i.e. component image node.
1811 */
1812 printf ("%s Image %u (%s)\n", p, count++,
1813 fit_get_name(fit, noffset, NULL));
1814
1815 fit_image_print (fit, noffset, p);
1816 }
1817 }
1818
1819 /* Find configurations parent node offset */
1820 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1821 if (confs_noffset < 0) {
1822 debug ("Can't get configurations parent node '%s' (%s)\n",
1823 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1824 return;
1825 }
1826
1827 /* get default configuration unit name from default property */
1828 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1829 if (uname)
1830 printf ("%s Default Configuration: '%s'\n", p, uname);
1831
1832 /* Process its subnodes, print out configurations details */
1833 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1834 (noffset >= 0) && (ndepth > 0);
1835 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1836 if (ndepth == 1) {
1837 /*
1838 * Direct child node of the configurations parent node,
1839 * i.e. configuration node.
1840 */
1841 printf ("%s Configuration %u (%s)\n", p, count++,
1842 fit_get_name(fit, noffset, NULL));
1843
1844 fit_conf_print (fit, noffset, p);
1845 }
1846 }
1847}
1848
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001849/**
1850 * fit_image_print - prints out the FIT component image details
1851 * @fit: pointer to the FIT format image header
1852 * @image_noffset: offset of the component image node
1853 * @p: pointer to prefix string
1854 *
1855 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001856 * image. If present, hash nodes are printed out as well. Load
1857 * address for images of type firmware is also printed out. Since the load
1858 * address is not mandatory for firmware images, it will be output as
1859 * "unavailable" when not present.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001860 *
1861 * returns:
1862 * no returned results
1863 */
1864void fit_image_print (const void *fit, int image_noffset, const char *p)
1865{
1866 char *desc;
1867 uint8_t type, arch, os, comp;
1868 size_t size;
1869 ulong load, entry;
1870 const void *data;
1871 int noffset;
1872 int ndepth;
1873 int ret;
1874
1875 /* Mandatory properties */
1876 ret = fit_get_desc (fit, image_noffset, &desc);
1877 printf ("%s Description: ", p);
1878 if (ret)
1879 printf ("unavailable\n");
1880 else
1881 printf ("%s\n", desc);
1882
1883 fit_image_get_type (fit, image_noffset, &type);
1884 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1885
1886 fit_image_get_comp (fit, image_noffset, &comp);
1887 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1888
1889 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1890
1891#ifndef USE_HOSTCC
1892 printf ("%s Data Start: ", p);
1893 if (ret)
1894 printf ("unavailable\n");
1895 else
1896 printf ("0x%08lx\n", (ulong)data);
1897#endif
1898
1899 printf ("%s Data Size: ", p);
1900 if (ret)
1901 printf ("unavailable\n");
1902 else
1903 genimg_print_size (size);
1904
1905 /* Remaining, type dependent properties */
1906 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1907 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1908 (type == IH_TYPE_FLATDT)) {
1909 fit_image_get_arch (fit, image_noffset, &arch);
1910 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1911 }
1912
1913 if (type == IH_TYPE_KERNEL) {
1914 fit_image_get_os (fit, image_noffset, &os);
1915 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1916 }
1917
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001918 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1919 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001920 ret = fit_image_get_load (fit, image_noffset, &load);
1921 printf ("%s Load Address: ", p);
1922 if (ret)
1923 printf ("unavailable\n");
1924 else
1925 printf ("0x%08lx\n", load);
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001926 }
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001927
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001928 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001929 fit_image_get_entry (fit, image_noffset, &entry);
1930 printf ("%s Entry Point: ", p);
1931 if (ret)
1932 printf ("unavailable\n");
1933 else
1934 printf ("0x%08lx\n", entry);
1935 }
1936
1937 /* Process all hash subnodes of the component image node */
1938 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1939 (noffset >= 0) && (ndepth > 0);
1940 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1941 if (ndepth == 1) {
1942 /* Direct child node of the component image node */
1943 fit_image_print_hash (fit, noffset, p);
1944 }
1945 }
1946}
1947
1948/**
1949 * fit_image_print_hash - prints out the hash node details
1950 * @fit: pointer to the FIT format image header
1951 * @noffset: offset of the hash node
1952 * @p: pointer to prefix string
1953 *
1954 * fit_image_print_hash() lists properies for the processed hash node
1955 *
1956 * returns:
1957 * no returned results
1958 */
1959void fit_image_print_hash (const void *fit, int noffset, const char *p)
1960{
1961 char *algo;
1962 uint8_t *value;
1963 int value_len;
1964 int i, ret;
1965
1966 /*
1967 * Check subnode name, must be equal to "hash".
1968 * Multiple hash nodes require unique unit node
1969 * names, e.g. hash@1, hash@2, etc.
1970 */
1971 if (strncmp (fit_get_name(fit, noffset, NULL),
1972 FIT_HASH_NODENAME,
1973 strlen(FIT_HASH_NODENAME)) != 0)
1974 return;
1975
1976 debug ("%s Hash node: '%s'\n", p,
1977 fit_get_name (fit, noffset, NULL));
1978
1979 printf ("%s Hash algo: ", p);
1980 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1981 printf ("invalid/unsupported\n");
1982 return;
1983 }
1984 printf ("%s\n", algo);
1985
1986 ret = fit_image_hash_get_value (fit, noffset, &value,
1987 &value_len);
1988 printf ("%s Hash value: ", p);
1989 if (ret) {
1990 printf ("unavailable\n");
1991 } else {
1992 for (i = 0; i < value_len; i++)
1993 printf ("%02x", value[i]);
1994 printf ("\n");
1995 }
1996
1997 debug ("%s Hash len: %d\n", p, value_len);
1998}
1999
2000/**
2001 * fit_get_desc - get node description property
2002 * @fit: pointer to the FIT format image header
2003 * @noffset: node offset
2004 * @desc: double pointer to the char, will hold pointer to the descrption
2005 *
2006 * fit_get_desc() reads description property from a given node, if
2007 * description is found pointer to it is returened in third call argument.
2008 *
2009 * returns:
2010 * 0, on success
2011 * -1, on failure
2012 */
2013int fit_get_desc (const void *fit, int noffset, char **desc)
2014{
2015 int len;
2016
2017 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2018 if (*desc == NULL) {
2019 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2020 return -1;
2021 }
2022
2023 return 0;
2024}
2025
2026/**
2027 * fit_get_timestamp - get node timestamp property
2028 * @fit: pointer to the FIT format image header
2029 * @noffset: node offset
2030 * @timestamp: pointer to the time_t, will hold read timestamp
2031 *
2032 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2033 * is found and has a correct size its value is retured in third call
2034 * argument.
2035 *
2036 * returns:
2037 * 0, on success
2038 * -1, on property read failure
2039 * -2, on wrong timestamp size
2040 */
2041int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2042{
2043 int len;
2044 const void *data;
2045
2046 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2047 if (data == NULL) {
2048 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2049 return -1;
2050 }
2051 if (len != sizeof (uint32_t)) {
2052 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2053 return -2;
2054 }
2055
2056 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2057 return 0;
2058}
2059
2060/**
2061 * fit_image_get_node - get node offset for component image of a given unit name
2062 * @fit: pointer to the FIT format image header
2063 * @image_uname: component image node unit name
2064 *
2065 * fit_image_get_node() finds a component image (withing the '/images'
2066 * node) of a provided unit name. If image is found its node offset is
2067 * returned to the caller.
2068 *
2069 * returns:
2070 * image node offset when found (>=0)
2071 * negative number on failure (FDT_ERR_* code)
2072 */
2073int fit_image_get_node (const void *fit, const char *image_uname)
2074{
2075 int noffset, images_noffset;
2076
2077 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2078 if (images_noffset < 0) {
2079 debug ("Can't find images parent node '%s' (%s)\n",
2080 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2081 return images_noffset;
2082 }
2083
2084 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2085 if (noffset < 0) {
2086 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2087 image_uname, fdt_strerror (noffset));
2088 }
2089
2090 return noffset;
2091}
2092
2093/**
2094 * fit_image_get_os - get os id for a given component image node
2095 * @fit: pointer to the FIT format image header
2096 * @noffset: component image node offset
2097 * @os: pointer to the uint8_t, will hold os numeric id
2098 *
2099 * fit_image_get_os() finds os property in a given component image node.
2100 * If the property is found, its (string) value is translated to the numeric
2101 * id which is returned to the caller.
2102 *
2103 * returns:
2104 * 0, on success
2105 * -1, on failure
2106 */
2107int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2108{
2109 int len;
2110 const void *data;
2111
2112 /* Get OS name from property data */
2113 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2114 if (data == NULL) {
2115 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2116 *os = -1;
2117 return -1;
2118 }
2119
2120 /* Translate OS name to id */
2121 *os = genimg_get_os_id (data);
2122 return 0;
2123}
2124
2125/**
2126 * fit_image_get_arch - get arch id for a given component image node
2127 * @fit: pointer to the FIT format image header
2128 * @noffset: component image node offset
2129 * @arch: pointer to the uint8_t, will hold arch numeric id
2130 *
2131 * fit_image_get_arch() finds arch property in a given component image node.
2132 * If the property is found, its (string) value is translated to the numeric
2133 * id which is returned to the caller.
2134 *
2135 * returns:
2136 * 0, on success
2137 * -1, on failure
2138 */
2139int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2140{
2141 int len;
2142 const void *data;
2143
2144 /* Get architecture name from property data */
2145 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2146 if (data == NULL) {
2147 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2148 *arch = -1;
2149 return -1;
2150 }
2151
2152 /* Translate architecture name to id */
2153 *arch = genimg_get_arch_id (data);
2154 return 0;
2155}
2156
2157/**
2158 * fit_image_get_type - get type id for a given component image node
2159 * @fit: pointer to the FIT format image header
2160 * @noffset: component image node offset
2161 * @type: pointer to the uint8_t, will hold type numeric id
2162 *
2163 * fit_image_get_type() finds type property in a given component image node.
2164 * If the property is found, its (string) value is translated to the numeric
2165 * id which is returned to the caller.
2166 *
2167 * returns:
2168 * 0, on success
2169 * -1, on failure
2170 */
2171int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2172{
2173 int len;
2174 const void *data;
2175
2176 /* Get image type name from property data */
2177 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2178 if (data == NULL) {
2179 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2180 *type = -1;
2181 return -1;
2182 }
2183
2184 /* Translate image type name to id */
2185 *type = genimg_get_type_id (data);
2186 return 0;
2187}
2188
2189/**
2190 * fit_image_get_comp - get comp id for a given component image node
2191 * @fit: pointer to the FIT format image header
2192 * @noffset: component image node offset
2193 * @comp: pointer to the uint8_t, will hold comp numeric id
2194 *
2195 * fit_image_get_comp() finds comp property in a given component image node.
2196 * If the property is found, its (string) value is translated to the numeric
2197 * id which is returned to the caller.
2198 *
2199 * returns:
2200 * 0, on success
2201 * -1, on failure
2202 */
2203int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2204{
2205 int len;
2206 const void *data;
2207
2208 /* Get compression name from property data */
2209 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2210 if (data == NULL) {
2211 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2212 *comp = -1;
2213 return -1;
2214 }
2215
2216 /* Translate compression name to id */
2217 *comp = genimg_get_comp_id (data);
2218 return 0;
2219}
2220
2221/**
2222 * fit_image_get_load - get load address property for a given component image node
2223 * @fit: pointer to the FIT format image header
2224 * @noffset: component image node offset
2225 * @load: pointer to the uint32_t, will hold load address
2226 *
2227 * fit_image_get_load() finds load address property in a given component image node.
2228 * If the property is found, its value is returned to the caller.
2229 *
2230 * returns:
2231 * 0, on success
2232 * -1, on failure
2233 */
2234int fit_image_get_load (const void *fit, int noffset, ulong *load)
2235{
2236 int len;
2237 const uint32_t *data;
2238
2239 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2240 if (data == NULL) {
2241 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2242 return -1;
2243 }
2244
2245 *load = uimage_to_cpu (*data);
2246 return 0;
2247}
2248
2249/**
2250 * fit_image_get_entry - get entry point address property for a given component image node
2251 * @fit: pointer to the FIT format image header
2252 * @noffset: component image node offset
2253 * @entry: pointer to the uint32_t, will hold entry point address
2254 *
2255 * fit_image_get_entry() finds entry point address property in a given component image node.
2256 * If the property is found, its value is returned to the caller.
2257 *
2258 * returns:
2259 * 0, on success
2260 * -1, on failure
2261 */
2262int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2263{
2264 int len;
2265 const uint32_t *data;
2266
2267 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2268 if (data == NULL) {
2269 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2270 return -1;
2271 }
2272
2273 *entry = uimage_to_cpu (*data);
2274 return 0;
2275}
2276
2277/**
2278 * fit_image_get_data - get data property and its size for a given component image node
2279 * @fit: pointer to the FIT format image header
2280 * @noffset: component image node offset
2281 * @data: double pointer to void, will hold data property's data address
2282 * @size: pointer to size_t, will hold data property's data size
2283 *
2284 * fit_image_get_data() finds data property in a given component image node.
2285 * If the property is found its data start address and size are returned to
2286 * the caller.
2287 *
2288 * returns:
2289 * 0, on success
2290 * -1, on failure
2291 */
2292int fit_image_get_data (const void *fit, int noffset,
2293 const void **data, size_t *size)
2294{
2295 int len;
2296
2297 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2298 if (*data == NULL) {
2299 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2300 *size = 0;
2301 return -1;
2302 }
2303
2304 *size = len;
2305 return 0;
2306}
2307
2308/**
2309 * fit_image_hash_get_algo - get hash algorithm name
2310 * @fit: pointer to the FIT format image header
2311 * @noffset: hash node offset
2312 * @algo: double pointer to char, will hold pointer to the algorithm name
2313 *
2314 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2315 * If the property is found its data start address is returned to the caller.
2316 *
2317 * returns:
2318 * 0, on success
2319 * -1, on failure
2320 */
2321int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2322{
2323 int len;
2324
2325 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2326 if (*algo == NULL) {
2327 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2328 return -1;
2329 }
2330
2331 return 0;
2332}
2333
2334/**
2335 * fit_image_hash_get_value - get hash value and length
2336 * @fit: pointer to the FIT format image header
2337 * @noffset: hash node offset
2338 * @value: double pointer to uint8_t, will hold address of a hash value data
2339 * @value_len: pointer to an int, will hold hash data length
2340 *
2341 * fit_image_hash_get_value() finds hash value property in a given hash node.
2342 * If the property is found its data start address and size are returned to
2343 * the caller.
2344 *
2345 * returns:
2346 * 0, on success
2347 * -1, on failure
2348 */
2349int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2350 int *value_len)
2351{
2352 int len;
2353
2354 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2355 if (*value == NULL) {
2356 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2357 *value_len = 0;
2358 return -1;
2359 }
2360
2361 *value_len = len;
2362 return 0;
2363}
2364
2365/**
2366 * fit_set_timestamp - set node timestamp property
2367 * @fit: pointer to the FIT format image header
2368 * @noffset: node offset
2369 * @timestamp: timestamp value to be set
2370 *
2371 * fit_set_timestamp() attempts to set timestamp property in the requested
2372 * node and returns operation status to the caller.
2373 *
2374 * returns:
2375 * 0, on success
2376 * -1, on property read failure
2377 */
2378int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2379{
2380 uint32_t t;
2381 int ret;
2382
2383 t = cpu_to_uimage (timestamp);
2384 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2385 sizeof (uint32_t));
2386 if (ret) {
2387 printf ("Can't set '%s' property for '%s' node (%s)\n",
2388 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2389 fdt_strerror (ret));
2390 return -1;
2391 }
2392
2393 return 0;
2394}
2395
2396/**
2397 * calculate_hash - calculate and return hash for provided input data
2398 * @data: pointer to the input data
2399 * @data_len: data length
2400 * @algo: requested hash algorithm
2401 * @value: pointer to the char, will hold hash value data (caller must
2402 * allocate enough free space)
2403 * value_len: length of the calculated hash
2404 *
2405 * calculate_hash() computes input data hash according to the requested algorithm.
2406 * Resulting hash value is placed in caller provided 'value' buffer, length
2407 * of the calculated hash is returned via value_len pointer argument.
2408 *
2409 * returns:
2410 * 0, on success
2411 * -1, when algo is unsupported
2412 */
2413static int calculate_hash (const void *data, int data_len, const char *algo,
2414 uint8_t *value, int *value_len)
2415{
2416 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002417 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2418 CHUNKSZ_CRC32);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002419 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2420 *value_len = 4;
2421 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002422 sha1_csum_wd ((unsigned char *) data, data_len,
2423 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002424 *value_len = 20;
2425 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002426 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01002427 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002428 } else {
2429 debug ("Unsupported hash alogrithm\n");
2430 return -1;
2431 }
2432 return 0;
2433}
2434
2435#ifdef USE_HOSTCC
2436/**
2437 * fit_set_hashes - process FIT component image nodes and calculate hashes
2438 * @fit: pointer to the FIT format image header
2439 *
2440 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2441 * Hashes are calculated for all component images which have hash subnodes
2442 * with algorithm property set to one of the supported hash algorithms.
2443 *
2444 * returns
2445 * 0, on success
2446 * libfdt error code, on failure
2447 */
2448int fit_set_hashes (void *fit)
2449{
2450 int images_noffset;
2451 int noffset;
2452 int ndepth;
2453 int ret;
2454
2455 /* Find images parent node offset */
2456 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2457 if (images_noffset < 0) {
2458 printf ("Can't find images parent node '%s' (%s)\n",
2459 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2460 return images_noffset;
2461 }
2462
2463 /* Process its subnodes, print out component images details */
2464 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2465 (noffset >= 0) && (ndepth > 0);
2466 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2467 if (ndepth == 1) {
2468 /*
2469 * Direct child node of the images parent node,
2470 * i.e. component image node.
2471 */
2472 ret = fit_image_set_hashes (fit, noffset);
2473 if (ret)
2474 return ret;
2475 }
2476 }
2477
2478 return 0;
2479}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01002480
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002481/**
2482 * fit_image_set_hashes - calculate/set hashes for given component image node
2483 * @fit: pointer to the FIT format image header
2484 * @image_noffset: requested component image node
2485 *
2486 * fit_image_set_hashes() adds hash values for an component image node. All
2487 * existing hash subnodes are checked, if algorithm property is set to one of
2488 * the supported hash algorithms, hash value is computed and corresponding
2489 * hash node property is set, for example:
2490 *
2491 * Input component image node structure:
2492 *
2493 * o image@1 (at image_noffset)
2494 * | - data = [binary data]
2495 * o hash@1
2496 * |- algo = "sha1"
2497 *
2498 * Output component image node structure:
2499 *
2500 * o image@1 (at image_noffset)
2501 * | - data = [binary data]
2502 * o hash@1
2503 * |- algo = "sha1"
2504 * |- value = sha1(data)
2505 *
2506 * returns:
2507 * 0 on sucess
2508 * <0 on failure
2509 */
2510int fit_image_set_hashes (void *fit, int image_noffset)
2511{
2512 const void *data;
2513 size_t size;
2514 char *algo;
2515 uint8_t value[FIT_MAX_HASH_LEN];
2516 int value_len;
2517 int noffset;
2518 int ndepth;
2519
2520 /* Get image data and data length */
2521 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2522 printf ("Can't get image data/size\n");
2523 return -1;
2524 }
2525
2526 /* Process all hash subnodes of the component image node */
2527 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2528 (noffset >= 0) && (ndepth > 0);
2529 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2530 if (ndepth == 1) {
2531 /* Direct child node of the component image node */
2532
2533 /*
2534 * Check subnode name, must be equal to "hash".
2535 * Multiple hash nodes require unique unit node
2536 * names, e.g. hash@1, hash@2, etc.
2537 */
2538 if (strncmp (fit_get_name(fit, noffset, NULL),
2539 FIT_HASH_NODENAME,
2540 strlen(FIT_HASH_NODENAME)) != 0) {
2541 /* Not a hash subnode, skip it */
2542 continue;
2543 }
2544
2545 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2546 printf ("Can't get hash algo property for "
2547 "'%s' hash node in '%s' image node\n",
2548 fit_get_name (fit, noffset, NULL),
2549 fit_get_name (fit, image_noffset, NULL));
2550 return -1;
2551 }
2552
2553 if (calculate_hash (data, size, algo, value, &value_len)) {
2554 printf ("Unsupported hash algorithm (%s) for "
2555 "'%s' hash node in '%s' image node\n",
2556 algo, fit_get_name (fit, noffset, NULL),
2557 fit_get_name (fit, image_noffset, NULL));
2558 return -1;
2559 }
2560
2561 if (fit_image_hash_set_value (fit, noffset, value,
2562 value_len)) {
2563 printf ("Can't set hash value for "
2564 "'%s' hash node in '%s' image node\n",
2565 fit_get_name (fit, noffset, NULL),
2566 fit_get_name (fit, image_noffset, NULL));
2567 return -1;
2568 }
2569 }
2570 }
2571
2572 return 0;
2573}
2574
2575/**
2576 * fit_image_hash_set_value - set hash value in requested has node
2577 * @fit: pointer to the FIT format image header
2578 * @noffset: hash node offset
2579 * @value: hash value to be set
2580 * @value_len: hash value length
2581 *
2582 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2583 * given and returns operation status to the caller.
2584 *
2585 * returns
2586 * 0, on success
2587 * -1, on failure
2588 */
2589int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2590 int value_len)
2591{
2592 int ret;
2593
2594 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2595 if (ret) {
2596 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2597 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2598 fdt_strerror (ret));
2599 return -1;
2600 }
2601
2602 return 0;
2603}
2604#endif /* USE_HOSTCC */
2605
2606/**
2607 * fit_image_check_hashes - verify data intergity
2608 * @fit: pointer to the FIT format image header
2609 * @image_noffset: component image node offset
2610 *
2611 * fit_image_check_hashes() goes over component image hash nodes,
2612 * re-calculates each data hash and compares with the value stored in hash
2613 * node.
2614 *
2615 * returns:
2616 * 1, if all hashes are valid
2617 * 0, otherwise (or on error)
2618 */
2619int fit_image_check_hashes (const void *fit, int image_noffset)
2620{
2621 const void *data;
2622 size_t size;
2623 char *algo;
2624 uint8_t *fit_value;
2625 int fit_value_len;
2626 uint8_t value[FIT_MAX_HASH_LEN];
2627 int value_len;
2628 int noffset;
2629 int ndepth;
2630 char *err_msg = "";
2631
2632 /* Get image data and data length */
2633 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2634 printf ("Can't get image data/size\n");
2635 return 0;
2636 }
2637
2638 /* Process all hash subnodes of the component image node */
2639 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2640 (noffset >= 0) && (ndepth > 0);
2641 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2642 if (ndepth == 1) {
2643 /* Direct child node of the component image node */
2644
2645 /*
2646 * Check subnode name, must be equal to "hash".
2647 * Multiple hash nodes require unique unit node
2648 * names, e.g. hash@1, hash@2, etc.
2649 */
2650 if (strncmp (fit_get_name(fit, noffset, NULL),
2651 FIT_HASH_NODENAME,
2652 strlen(FIT_HASH_NODENAME)) != 0)
2653 continue;
2654
2655 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002656 err_msg = " error!\nCan't get hash algo "
2657 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002658 goto error;
2659 }
2660 printf ("%s", algo);
2661
2662 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2663 &fit_value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002664 err_msg = " error!\nCan't get hash value "
2665 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002666 goto error;
2667 }
2668
2669 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002670 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002671 goto error;
2672 }
2673
2674 if (value_len != fit_value_len) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002675 err_msg = " error !\nBad hash value len";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002676 goto error;
2677 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002678 err_msg = " error!\nBad hash value";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002679 goto error;
2680 }
2681 printf ("+ ");
2682 }
2683 }
2684
2685 return 1;
2686
2687error:
2688 printf ("%s for '%s' hash node in '%s' image node\n",
2689 err_msg, fit_get_name (fit, noffset, NULL),
2690 fit_get_name (fit, image_noffset, NULL));
2691 return 0;
2692}
2693
2694/**
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002695 * fit_all_image_check_hashes - verify data intergity for all images
2696 * @fit: pointer to the FIT format image header
2697 *
2698 * fit_all_image_check_hashes() goes over all images in the FIT and
2699 * for every images checks if all it's hashes are valid.
2700 *
2701 * returns:
2702 * 1, if all hashes of all images are valid
2703 * 0, otherwise (or on error)
2704 */
2705int fit_all_image_check_hashes (const void *fit)
2706{
2707 int images_noffset;
2708 int noffset;
2709 int ndepth;
2710 int count;
2711
2712 /* Find images parent node offset */
2713 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2714 if (images_noffset < 0) {
2715 printf ("Can't find images parent node '%s' (%s)\n",
2716 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2717 return 0;
2718 }
2719
2720 /* Process all image subnodes, check hashes for each */
2721 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2722 (ulong)fit);
2723 for (ndepth = 0, count = 0,
2724 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2725 (noffset >= 0) && (ndepth > 0);
2726 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2727 if (ndepth == 1) {
2728 /*
2729 * Direct child node of the images parent node,
2730 * i.e. component image node.
2731 */
2732 printf (" Hash(es) for Image %u (%s): ", count++,
2733 fit_get_name (fit, noffset, NULL));
2734
2735 if (!fit_image_check_hashes (fit, noffset))
2736 return 0;
2737 printf ("\n");
2738 }
2739 }
2740 return 1;
2741}
2742
2743/**
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002744 * fit_image_check_os - check whether image node is of a given os type
2745 * @fit: pointer to the FIT format image header
2746 * @noffset: component image node offset
2747 * @os: requested image os
2748 *
2749 * fit_image_check_os() reads image os property and compares its numeric
2750 * id with the requested os. Comparison result is returned to the caller.
2751 *
2752 * returns:
2753 * 1 if image is of given os type
2754 * 0 otherwise (or on error)
2755 */
2756int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2757{
2758 uint8_t image_os;
2759
2760 if (fit_image_get_os (fit, noffset, &image_os))
2761 return 0;
2762 return (os == image_os);
2763}
2764
2765/**
2766 * fit_image_check_arch - check whether image node is of a given arch
2767 * @fit: pointer to the FIT format image header
2768 * @noffset: component image node offset
2769 * @arch: requested imagearch
2770 *
2771 * fit_image_check_arch() reads image arch property and compares its numeric
2772 * id with the requested arch. Comparison result is returned to the caller.
2773 *
2774 * returns:
2775 * 1 if image is of given arch
2776 * 0 otherwise (or on error)
2777 */
2778int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2779{
2780 uint8_t image_arch;
2781
2782 if (fit_image_get_arch (fit, noffset, &image_arch))
2783 return 0;
2784 return (arch == image_arch);
2785}
2786
2787/**
2788 * fit_image_check_type - check whether image node is of a given type
2789 * @fit: pointer to the FIT format image header
2790 * @noffset: component image node offset
2791 * @type: requested image type
2792 *
2793 * fit_image_check_type() reads image type property and compares its numeric
2794 * id with the requested type. Comparison result is returned to the caller.
2795 *
2796 * returns:
2797 * 1 if image is of given type
2798 * 0 otherwise (or on error)
2799 */
2800int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2801{
2802 uint8_t image_type;
2803
2804 if (fit_image_get_type (fit, noffset, &image_type))
2805 return 0;
2806 return (type == image_type);
2807}
2808
2809/**
2810 * fit_image_check_comp - check whether image node uses given compression
2811 * @fit: pointer to the FIT format image header
2812 * @noffset: component image node offset
2813 * @comp: requested image compression type
2814 *
2815 * fit_image_check_comp() reads image compression property and compares its
2816 * numeric id with the requested compression type. Comparison result is
2817 * returned to the caller.
2818 *
2819 * returns:
2820 * 1 if image uses requested compression
2821 * 0 otherwise (or on error)
2822 */
2823int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2824{
2825 uint8_t image_comp;
2826
2827 if (fit_image_get_comp (fit, noffset, &image_comp))
2828 return 0;
2829 return (comp == image_comp);
2830}
2831
2832/**
2833 * fit_check_format - sanity check FIT image format
2834 * @fit: pointer to the FIT format image header
2835 *
2836 * fit_check_format() runs a basic sanity FIT image verification.
2837 * Routine checks for mandatory properties, nodes, etc.
2838 *
2839 * returns:
2840 * 1, on success
2841 * 0, on failure
2842 */
2843int fit_check_format (const void *fit)
2844{
2845 /* mandatory / node 'description' property */
2846 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2847 debug ("Wrong FIT format: no description\n");
2848 return 0;
2849 }
2850
2851#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2852 /* mandatory / node 'timestamp' property */
2853 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002854 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002855 return 0;
2856 }
2857#endif
2858
2859 /* mandatory subimages parent '/images' node */
2860 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2861 debug ("Wrong FIT format: no images parent node\n");
2862 return 0;
2863 }
2864
2865 return 1;
2866}
2867
2868/**
2869 * fit_conf_get_node - get node offset for configuration of a given unit name
2870 * @fit: pointer to the FIT format image header
2871 * @conf_uname: configuration node unit name
2872 *
2873 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2874 * parant node) of a provided unit name. If configuration is found its node offset
2875 * is returned to the caller.
2876 *
2877 * When NULL is provided in second argument fit_conf_get_node() will search
2878 * for a default configuration node instead. Default configuration node unit name
2879 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2880 *
2881 * returns:
2882 * configuration node offset when found (>=0)
2883 * negative number on failure (FDT_ERR_* code)
2884 */
2885int fit_conf_get_node (const void *fit, const char *conf_uname)
2886{
2887 int noffset, confs_noffset;
2888 int len;
2889
2890 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2891 if (confs_noffset < 0) {
2892 debug ("Can't find configurations parent node '%s' (%s)\n",
2893 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2894 return confs_noffset;
2895 }
2896
2897 if (conf_uname == NULL) {
2898 /* get configuration unit name from the default property */
2899 debug ("No configuration specified, trying default...\n");
2900 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2901 if (conf_uname == NULL) {
2902 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2903 return len;
2904 }
2905 debug ("Found default configuration: '%s'\n", conf_uname);
2906 }
2907
2908 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2909 if (noffset < 0) {
2910 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2911 conf_uname, fdt_strerror (noffset));
2912 }
2913
2914 return noffset;
2915}
2916
2917static int __fit_conf_get_prop_node (const void *fit, int noffset,
2918 const char *prop_name)
2919{
2920 char *uname;
2921 int len;
2922
2923 /* get kernel image unit name from configuration kernel property */
2924 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2925 if (uname == NULL)
2926 return len;
2927
2928 return fit_image_get_node (fit, uname);
2929}
2930
2931/**
2932 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2933 * a given configuration
2934 * @fit: pointer to the FIT format image header
2935 * @noffset: configuration node offset
2936 *
2937 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2938 * configuration FIT_KERNEL_PROP property and translates it to the node
2939 * offset.
2940 *
2941 * returns:
2942 * image node offset when found (>=0)
2943 * negative number on failure (FDT_ERR_* code)
2944 */
2945int fit_conf_get_kernel_node (const void *fit, int noffset)
2946{
2947 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2948}
2949
2950/**
2951 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2952 * a given configuration
2953 * @fit: pointer to the FIT format image header
2954 * @noffset: configuration node offset
2955 *
2956 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2957 * configuration FIT_KERNEL_PROP property and translates it to the node
2958 * offset.
2959 *
2960 * returns:
2961 * image node offset when found (>=0)
2962 * negative number on failure (FDT_ERR_* code)
2963 */
2964int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2965{
2966 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2967}
2968
2969/**
2970 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2971 * a given configuration
2972 * @fit: pointer to the FIT format image header
2973 * @noffset: configuration node offset
2974 *
2975 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2976 * configuration FIT_KERNEL_PROP property and translates it to the node
2977 * offset.
2978 *
2979 * returns:
2980 * image node offset when found (>=0)
2981 * negative number on failure (FDT_ERR_* code)
2982 */
2983int fit_conf_get_fdt_node (const void *fit, int noffset)
2984{
2985 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2986}
2987
2988/**
2989 * fit_conf_print - prints out the FIT configuration details
2990 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01002991 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002992 * @p: pointer to prefix string
2993 *
2994 * fit_conf_print() lists all mandatory properies for the processed
2995 * configuration node.
2996 *
2997 * returns:
2998 * no returned results
2999 */
3000void fit_conf_print (const void *fit, int noffset, const char *p)
3001{
3002 char *desc;
3003 char *uname;
3004 int ret;
3005
3006 /* Mandatory properties */
3007 ret = fit_get_desc (fit, noffset, &desc);
3008 printf ("%s Description: ", p);
3009 if (ret)
3010 printf ("unavailable\n");
3011 else
3012 printf ("%s\n", desc);
3013
3014 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3015 printf ("%s Kernel: ", p);
3016 if (uname == NULL)
3017 printf ("unavailable\n");
3018 else
3019 printf ("%s\n", uname);
3020
3021 /* Optional properties */
3022 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3023 if (uname)
3024 printf ("%s Init Ramdisk: %s\n", p, uname);
3025
3026 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3027 if (uname)
3028 printf ("%s FDT: %s\n", p, uname);
3029}
Marian Balakowicza50d5152008-03-12 10:12:37 +01003030
3031/**
3032 * fit_check_ramdisk - verify FIT format ramdisk subimage
3033 * @fit_hdr: pointer to the FIT ramdisk header
3034 * @rd_noffset: ramdisk subimage node offset within FIT image
3035 * @arch: requested ramdisk image architecture type
3036 * @verify: data CRC verification flag
3037 *
3038 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3039 * specified FIT image.
3040 *
3041 * returns:
3042 * 1, on success
3043 * 0, on failure
3044 */
3045#ifndef USE_HOSTCC
3046static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3047{
3048 fit_image_print (fit, rd_noffset, " ");
3049
3050 if (verify) {
3051 puts (" Verifying Hash Integrity ... ");
3052 if (!fit_image_check_hashes (fit, rd_noffset)) {
3053 puts ("Bad Data Hash\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003054 show_boot_progress (-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003055 return 0;
3056 }
3057 puts ("OK\n");
3058 }
3059
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003060 show_boot_progress (126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003061 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3062 !fit_image_check_arch (fit, rd_noffset, arch) ||
3063 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3064 printf ("No Linux %s Ramdisk Image\n",
3065 genimg_get_arch_name(arch));
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003066 show_boot_progress (-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003067 return 0;
3068 }
3069
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003070 show_boot_progress (127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003071 return 1;
3072}
3073#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01003074#endif /* CONFIG_FIT */