blob: 9e4971303e81e18f596ae7ce08d6a5cf7818a3c9 [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",},
Stefano Babic7b07f092010-01-20 18:19:10 +0100143 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100144 { -1, "", "", },
145};
146
147static table_entry_t uimage_comp[] = {
148 { IH_COMP_NONE, "none", "uncompressed", },
149 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
150 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +0200151 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100152 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100153 { -1, "", "", },
154};
155
Mike Frysingera2c67e92008-03-31 11:02:01 -0400156uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200157uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100158#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
159static void genimg_print_time (time_t timestamp);
160#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100161
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100162/*****************************************************************************/
163/* Legacy format routines */
164/*****************************************************************************/
Wolfgang Denk3b506772009-08-19 11:42:56 +0200165int image_check_hcrc (const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100166{
167 ulong hcrc;
168 ulong len = image_get_header_size ();
169 image_header_t header;
170
171 /* Copy header so we can blank CRC field for re-calculation */
172 memmove (&header, (char *)hdr, image_get_header_size ());
173 image_set_hcrc (&header, 0);
174
175 hcrc = crc32 (0, (unsigned char *)&header, len);
176
177 return (hcrc == image_get_hcrc (hdr));
178}
179
Wolfgang Denk3b506772009-08-19 11:42:56 +0200180int image_check_dcrc (const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100181{
182 ulong data = image_get_data (hdr);
183 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200184 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100185
186 return (dcrc == image_get_dcrc (hdr));
187}
188
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100189/**
190 * image_multi_count - get component (sub-image) count
191 * @hdr: pointer to the header of the multi component image
192 *
193 * image_multi_count() returns number of components in a multi
194 * component image.
195 *
196 * Note: no checking of the image type is done, caller must pass
197 * a valid multi component image.
198 *
199 * returns:
200 * number of components
201 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200202ulong image_multi_count (const image_header_t *hdr)
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100203{
204 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100205 uint32_t *size;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100206
207 /* get start of the image payload, which in case of multi
208 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100209 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100210
211 /* count non empty slots */
212 for (i = 0; size[i]; ++i)
213 count++;
214
215 return count;
216}
217
218/**
219 * image_multi_getimg - get component data address and size
220 * @hdr: pointer to the header of the multi component image
221 * @idx: index of the requested component
222 * @data: pointer to a ulong variable, will hold component data address
223 * @len: pointer to a ulong variable, will hold component size
224 *
225 * image_multi_getimg() returns size and data address for the requested
226 * component in a multi component image.
227 *
228 * Note: no checking of the image type is done, caller must pass
229 * a valid multi component image.
230 *
231 * returns:
232 * data address and size of the component, if idx is valid
233 * 0 in data and len, if idx is out of range
234 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200235void image_multi_getimg (const image_header_t *hdr, ulong idx,
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100236 ulong *data, ulong *len)
237{
238 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100239 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700240 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100241
242 /* get number of component */
243 count = image_multi_count (hdr);
244
245 /* get start of the image payload, which in case of multi
246 * component images that points to a table of component sizes */
Marian Balakowicza1474212008-02-29 16:00:06 +0100247 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100248
249 /* get address of the proper component data start, which means
250 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowicza1474212008-02-29 16:00:06 +0100251 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100252
253 if (idx < count) {
Marian Balakowicza1474212008-02-29 16:00:06 +0100254 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100255 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100256
257 /* go over all indices preceding requested component idx */
258 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-05-10 14:02:04 -0700259 /* add up i-th component size, rounding up to 4 bytes */
260 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100261 }
262
263 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700264 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100265 } else {
266 *len = 0;
267 *data = 0;
268 }
269}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100270
Wolfgang Denk3b506772009-08-19 11:42:56 +0200271static void image_print_type (const image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100272{
273 const char *os, *arch, *type, *comp;
274
275 os = genimg_get_os_name (image_get_os (hdr));
276 arch = genimg_get_arch_name (image_get_arch (hdr));
277 type = genimg_get_type_name (image_get_type (hdr));
278 comp = genimg_get_comp_name (image_get_comp (hdr));
279
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100280 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100281}
282
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100283/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200284 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3b506772009-08-19 11:42:56 +0200285 * @ptr: pointer to the legacy format image header
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100286 * @p: pointer to prefix string
287 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200288 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100289 * The routine prints out all header fields followed by the size/offset data
290 * for MULTI/SCRIPT images.
291 *
292 * returns:
293 * no returned results
294 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200295void image_print_contents (const void *ptr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100296{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200297 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200298 const char *p;
299
300#ifdef USE_HOSTCC
301 p = "";
302#else
303 p = " ";
304#endif
305
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100306 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
307#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
308 printf ("%sCreated: ", p);
309 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100310#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100311 printf ("%sImage Type: ", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100312 image_print_type (hdr);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100313 printf ("%sData Size: ", p);
314 genimg_print_size (image_get_data_size (hdr));
315 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
316 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100317
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100318 if (image_check_type (hdr, IH_TYPE_MULTI) ||
319 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100320 int i;
321 ulong data, len;
322 ulong count = image_multi_count (hdr);
323
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100324 printf ("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100325 for (i = 0; i < count; i++) {
326 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100327
328 printf ("%s Image %d: ", p, i);
329 genimg_print_size (len);
330
331 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
332 /*
333 * the user may need to know offsets
334 * if planning to do something with
335 * multiple files
336 */
337 printf ("%s Offset = 0x%08lx\n", p, data);
338 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100339 }
340 }
341}
342
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100343
344#ifndef USE_HOSTCC
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100345/**
346 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100347 * @rd_addr: ramdisk image start address
348 * @arch: expected ramdisk architecture
349 * @verify: checksum verification flag
350 *
351 * image_get_ramdisk() returns a pointer to the verified ramdisk image
352 * header. Routine receives image start address and expected architecture
353 * flag. Verification done covers data and header integrity and os/type/arch
354 * fields checking.
355 *
356 * If dataflash support is enabled routine checks for dataflash addresses
357 * and handles required dataflash reads.
358 *
359 * returns:
360 * pointer to a ramdisk image header, if image was found and valid
361 * otherwise, return NULL
362 */
Wolfgang Denk3b506772009-08-19 11:42:56 +0200363static const image_header_t *image_get_ramdisk (ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100364 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100365{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200366 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100367
368 if (!image_check_magic (rd_hdr)) {
369 puts ("Bad Magic Number\n");
370 show_boot_progress (-10);
371 return NULL;
372 }
373
374 if (!image_check_hcrc (rd_hdr)) {
375 puts ("Bad Header Checksum\n");
376 show_boot_progress (-11);
377 return NULL;
378 }
379
380 show_boot_progress (10);
381 image_print_contents (rd_hdr);
382
383 if (verify) {
384 puts(" Verifying Checksum ... ");
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +0200385 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100386 puts ("Bad Data CRC\n");
387 show_boot_progress (-12);
388 return NULL;
389 }
390 puts("OK\n");
391 }
392
393 show_boot_progress (11);
394
395 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
396 !image_check_arch (rd_hdr, arch) ||
397 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
398 printf ("No Linux %s Ramdisk Image\n",
399 genimg_get_arch_name(arch));
400 show_boot_progress (-13);
401 return NULL;
402 }
403
404 return rd_hdr;
405}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100406#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100407
408/*****************************************************************************/
409/* Shared dual-format routines */
410/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100411#ifndef USE_HOSTCC
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200412int getenv_yesno (char *var)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100413{
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200414 char *s = getenv (var);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100415 return (s && (*s == 'n')) ? 0 : 1;
416}
417
418ulong getenv_bootm_low(void)
419{
420 char *s = getenv ("bootm_low");
421 if (s) {
422 ulong tmp = simple_strtoul (s, NULL, 16);
423 return tmp;
424 }
425
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200426#if defined(CONFIG_SYS_SDRAM_BASE)
427 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100428#elif defined(CONFIG_ARM)
429 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100430#else
431 return 0;
432#endif
433}
434
Becky Bruced26d67c2008-06-09 20:37:18 -0500435phys_size_t getenv_bootm_size(void)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100436{
437 char *s = getenv ("bootm_size");
438 if (s) {
Becky Bruced26d67c2008-06-09 20:37:18 -0500439 phys_size_t tmp;
Becky Bruced26d67c2008-06-09 20:37:18 -0500440 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100441 return tmp;
442 }
443
Marian Balakowicz2499b042008-03-12 12:14:15 +0100444#if defined(CONFIG_ARM)
445 return gd->bd->bi_dram[0].size;
446#else
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100447 return gd->bd->bi_memsize;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100448#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100449}
450
451void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
452{
453#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
454 while (len > 0) {
455 size_t tail = (len > chunksz) ? chunksz : len;
456 WATCHDOG_RESET ();
457 memmove (to, from, tail);
458 to += tail;
459 from += tail;
460 len -= tail;
461 }
462#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
463 memmove (to, from, len);
464#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
465}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100466#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100467
Prafulla Wadaskar49432ab2009-09-07 14:59:07 +0530468void genimg_print_size (uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100469{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100470#ifndef USE_HOSTCC
471 printf ("%d Bytes = ", size);
472 print_size (size, "\n");
473#else
474 printf ("%d Bytes = %.2f kB = %.2f MB\n",
475 size, (double)size / 1.024e3,
476 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100477#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100478}
479
480#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
481static void genimg_print_time (time_t timestamp)
482{
483#ifndef USE_HOSTCC
484 struct rtc_time tm;
485
486 to_tm (timestamp, &tm);
487 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
488 tm.tm_year, tm.tm_mon, tm.tm_mday,
489 tm.tm_hour, tm.tm_min, tm.tm_sec);
490#else
491 printf ("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100492#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100493}
494#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
495
496/**
497 * get_table_entry_name - translate entry id to long name
498 * @table: pointer to a translation table for entries of a specific type
499 * @msg: message to be returned when translation fails
500 * @id: entry id to be translated
501 *
502 * get_table_entry_name() will go over translation table trying to find
503 * entry that matches given id. If matching entry is found, its long
504 * name is returned to the caller.
505 *
506 * returns:
507 * long entry name if translation succeeds
508 * msg otherwise
509 */
Prafulla Wadaskar4454c9e2009-09-07 14:59:08 +0530510char *get_table_entry_name (table_entry_t *table, char *msg, int id)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100511{
512 for (; table->id >= 0; ++table) {
513 if (table->id == id)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500514#if defined(USE_HOSTCC) || defined(CONFIG_RELOC_FIXUP_WORKS)
Scott Wood68498ab2009-04-02 16:15:10 -0500515 return table->lname;
516#else
517 return table->lname + gd->reloc_off;
518#endif
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100519 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100520 return (msg);
521}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100522
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100523const char *genimg_get_os_name (uint8_t os)
524{
525 return (get_table_entry_name (uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100526}
527
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100528const char *genimg_get_arch_name (uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100529{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100530 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
531}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100532
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100533const char *genimg_get_type_name (uint8_t type)
534{
535 return (get_table_entry_name (uimage_type, "Unknown Image", type));
536}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100537
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100538const char *genimg_get_comp_name (uint8_t comp)
539{
540 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100541}
542
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100543/**
544 * get_table_entry_id - translate short entry name to id
545 * @table: pointer to a translation table for entries of a specific type
546 * @table_name: to be used in case of error
547 * @name: entry short name to be translated
548 *
549 * get_table_entry_id() will go over translation table trying to find
550 * entry that matches given short name. If matching entry is found,
551 * its id returned to the caller.
552 *
553 * returns:
554 * entry id if translation succeeds
555 * -1 otherwise
556 */
Prafulla Wadaskar4454c9e2009-09-07 14:59:08 +0530557int get_table_entry_id (table_entry_t *table,
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100558 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100559{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100560 table_entry_t *t;
561#ifdef USE_HOSTCC
562 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100563
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100564 for (t = table; t->id >= 0; ++t) {
565 if (t->sname && strcasecmp(t->sname, name) == 0)
566 return (t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100567 }
568
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100569 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
570 for (t = table; t->id >= 0; ++t) {
571 if (t->sname == NULL)
572 continue;
573 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
574 first = 0;
575 }
576 fprintf (stderr, "\n");
577#else
578 for (t = table; t->id >= 0; ++t) {
Peter Tyser9057cbf2009-09-21 11:20:36 -0500579#ifdef CONFIG_RELOC_FIXUP_WORKS
580 if (t->sname && strcmp(t->sname, name) == 0)
581#else
Scott Wood68498ab2009-04-02 16:15:10 -0500582 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500583#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100584 return (t->id);
585 }
586 debug ("Invalid %s Type: %s\n", table_name, name);
587#endif /* USE_HOSTCC */
588 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100589}
590
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100591int genimg_get_os_id (const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100592{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100593 return (get_table_entry_id (uimage_os, "OS", name));
594}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100595
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100596int genimg_get_arch_id (const char *name)
597{
598 return (get_table_entry_id (uimage_arch, "CPU", name));
599}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100600
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100601int genimg_get_type_id (const char *name)
602{
603 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100604}
Marian Balakowicz95418502008-01-31 13:55:39 +0100605
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100606int genimg_get_comp_id (const char *name)
607{
608 return (get_table_entry_id (uimage_comp, "Compression", name));
609}
610
611#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100612/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100613 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100614 * @img_addr: image start address
615 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100616 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100617 * legacy or FIT image.
618 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100619 * New uImage format and FDT blob are based on a libfdt. FDT blob
620 * may be passed directly or embedded in a FIT image. In both situations
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100621 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100622 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100623 * returns:
624 * image format type or IMAGE_FORMAT_INVALID if no image is present
625 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100626int genimg_get_format (void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100627{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200628 ulong format = IMAGE_FORMAT_INVALID;
629 const image_header_t *hdr;
Marian Balakowicz12560682008-02-27 11:02:26 +0100630#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3b506772009-08-19 11:42:56 +0200631 char *fit_hdr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100632#endif
633
Wolfgang Denk3b506772009-08-19 11:42:56 +0200634 hdr = (const image_header_t *)img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100635 if (image_check_magic(hdr))
636 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz12560682008-02-27 11:02:26 +0100637#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100638 else {
639 fit_hdr = (char *)img_addr;
640 if (fdt_check_header (fit_hdr) == 0)
641 format = IMAGE_FORMAT_FIT;
642 }
643#endif
644
645 return format;
646}
647
648/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100649 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100650 * @img_addr: image start address
651 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100652 * genimg_get_image() checks if provided image start adddress is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100653 * in a dataflash storage. If so, image is moved to a system RAM memory.
654 *
655 * returns:
656 * image start address after possible relocation from special storage
657 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100658ulong genimg_get_image (ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100659{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100660 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100661
662#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100663 ulong h_size, d_size;
664
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100665 if (addr_dataflash (img_addr)){
Marian Balakowicz535b9102008-02-27 11:00:47 +0100666 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200667 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100668
669 /* get header size */
670 h_size = image_get_header_size ();
671#if defined(CONFIG_FIT)
672 if (sizeof(struct fdt_header) > h_size)
673 h_size = sizeof(struct fdt_header);
674#endif
675
676 /* read in header */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100677 debug (" Reading image header from dataflash address "
678 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100679
Marian Balakowicz535b9102008-02-27 11:00:47 +0100680 read_dataflash (img_addr, h_size, (char *)ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100681
Marian Balakowicz535b9102008-02-27 11:00:47 +0100682 /* get data size */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100683 switch (genimg_get_format ((void *)ram_addr)) {
Marian Balakowicz535b9102008-02-27 11:00:47 +0100684 case IMAGE_FORMAT_LEGACY:
Wolfgang Denk3b506772009-08-19 11:42:56 +0200685 d_size = image_get_data_size ((const image_header_t *)ram_addr);
Marian Balakowicz535b9102008-02-27 11:00:47 +0100686 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
687 ram_addr, d_size);
688 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100689#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100690 case IMAGE_FORMAT_FIT:
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100691 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100692 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
693 ram_addr, d_size);
694 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100695#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100696 default:
697 printf (" No valid image found at 0x%08lx\n", img_addr);
698 return ram_addr;
699 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100700
Marian Balakowicz535b9102008-02-27 11:00:47 +0100701 /* read in image data */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100702 debug (" Reading image remaining data from dataflash address "
703 "%08lx to RAM address %08lx\n", img_addr + h_size,
704 ram_addr + h_size);
705
706 read_dataflash (img_addr + h_size, d_size,
707 (char *)(ram_addr + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100708
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100709 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100710#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100711
712 return ram_addr;
713}
714
715/**
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100716 * fit_has_config - check if there is a valid FIT configuration
717 * @images: pointer to the bootm command headers structure
718 *
719 * fit_has_config() checks if there is a FIT configuration in use
720 * (if FTI support is present).
721 *
722 * returns:
723 * 0, no FIT support or no configuration found
724 * 1, configuration found
725 */
726int genimg_has_config (bootm_headers_t *images)
727{
728#if defined(CONFIG_FIT)
729 if (images->fit_uname_cfg)
730 return 1;
731#endif
732 return 0;
733}
734
735/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100736 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100737 * @argc: command argument count
738 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100739 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-01-31 13:55:39 +0100740 * @arch: expected ramdisk architecture
741 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
742 * @rd_end: pointer to a ulong variable, will hold ramdisk end
743 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100744 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-01-31 13:55:39 +0100745 * Curently supported are the following ramdisk sources:
746 * - multicomponent kernel/ramdisk image,
747 * - commandline provided address of decicated ramdisk image.
748 *
749 * returns:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100750 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-01-31 13:55:39 +0100751 * rd_start and rd_end are set to ramdisk start/end addresses if
752 * ramdisk image is found and valid
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100753 *
Kumar Galab8ca7342008-08-29 19:08:29 -0500754 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz95418502008-01-31 13:55:39 +0100755 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100756 */
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100757int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
758 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100759{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100760 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100761 ulong rd_data, rd_len;
Wolfgang Denk3b506772009-08-19 11:42:56 +0200762 const image_header_t *rd_hdr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100763#if defined(CONFIG_FIT)
764 void *fit_hdr;
765 const char *fit_uname_config = NULL;
766 const char *fit_uname_ramdisk = NULL;
767 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100768 int rd_noffset;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100769 int cfg_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100770 const void *data;
771 size_t size;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100772#endif
Marian Balakowicz95418502008-01-31 13:55:39 +0100773
Marian Balakowicza50d5152008-03-12 10:12:37 +0100774 *rd_start = 0;
775 *rd_end = 0;
776
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100777 /*
778 * Look for a '-' which indicates to ignore the
779 * ramdisk argument
780 */
781 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
782 debug ("## Skipping init Ramdisk\n");
783 rd_len = rd_data = 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100784 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100785#if defined(CONFIG_FIT)
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100786 if (argc >= 3) {
787 /*
788 * If the init ramdisk comes from the FIT image and
789 * the FIT image address is omitted in the command
790 * line argument, try to use os FIT image address or
791 * default load address.
792 */
793 if (images->fit_uname_os)
794 default_addr = (ulong)images->fit_hdr_os;
795 else
796 default_addr = load_addr;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100797
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100798 if (fit_parse_conf (argv[2], default_addr,
799 &rd_addr, &fit_uname_config)) {
800 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
801 fit_uname_config, rd_addr);
802 } else if (fit_parse_subimage (argv[2], default_addr,
803 &rd_addr, &fit_uname_ramdisk)) {
804 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
805 fit_uname_ramdisk, rd_addr);
806 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100807#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100808 {
809 rd_addr = simple_strtoul(argv[2], NULL, 16);
810 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
811 rd_addr);
812 }
813#if defined(CONFIG_FIT)
814 } else {
815 /* use FIT configuration provided in first bootm
816 * command argument
817 */
818 rd_addr = (ulong)images->fit_hdr_os;
819 fit_uname_config = images->fit_uname_cfg;
820 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
821 fit_uname_config, rd_addr);
822
823 /*
824 * Check whether configuration has ramdisk defined,
825 * if not, don't try to use it, quit silently.
826 */
827 fit_hdr = (void *)rd_addr;
828 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
829 if (cfg_noffset < 0) {
830 debug ("* ramdisk: no such config\n");
Michal Simekb2474002008-07-11 10:43:13 +0200831 return 1;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100832 }
833
834 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
835 if (rd_noffset < 0) {
836 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser190ee482008-08-05 10:51:57 -0500837 return 0;
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100838 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100839 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100840#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100841
842 /* copy from dataflash if needed */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100843 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100844
845 /*
846 * Check if there is an initrd image at the
847 * address provided in the second bootm argument
848 * check image type, for FIT images get FIT node.
849 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100850 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100851 case IMAGE_FORMAT_LEGACY:
Marian Balakowicza50d5152008-03-12 10:12:37 +0100852 printf ("## Loading init Ramdisk from Legacy "
853 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100854
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100855 show_boot_progress (9);
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100856 rd_hdr = image_get_ramdisk (rd_addr, arch,
857 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100858
Marian Balakowicza50d5152008-03-12 10:12:37 +0100859 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600860 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600861
Marian Balakowicz95418502008-01-31 13:55:39 +0100862 rd_data = image_get_data (rd_hdr);
863 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100864 rd_load = image_get_load (rd_hdr);
865 break;
866#if defined(CONFIG_FIT)
867 case IMAGE_FORMAT_FIT:
868 fit_hdr = (void *)rd_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100869 printf ("## Loading init Ramdisk from FIT "
870 "Image at %08lx ...\n", rd_addr);
871
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100872 show_boot_progress (120);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100873 if (!fit_check_format (fit_hdr)) {
874 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100875 show_boot_progress (-120);
Michal Simekb2474002008-07-11 10:43:13 +0200876 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100877 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100878 show_boot_progress (121);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100879
880 if (!fit_uname_ramdisk) {
881 /*
882 * no ramdisk image node unit name, try to get config
883 * node first. If config unit node name is NULL
884 * fit_conf_get_node() will try to find default config node
885 */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100886 show_boot_progress (122);
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100887 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
888 if (cfg_noffset < 0) {
889 puts ("Could not find configuration node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100890 show_boot_progress (-122);
Michal Simekb2474002008-07-11 10:43:13 +0200891 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100892 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100893 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
894 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100895
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100896 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100897 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
898 } else {
899 /* get ramdisk component image node offset */
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100900 show_boot_progress (123);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100901 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
902 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100903 if (rd_noffset < 0) {
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100904 puts ("Could not find subimage node\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100905 show_boot_progress (-124);
Michal Simekb2474002008-07-11 10:43:13 +0200906 return 1;
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100907 }
Marian Balakowicza50d5152008-03-12 10:12:37 +0100908
909 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
910
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100911 show_boot_progress (125);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100912 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekb2474002008-07-11 10:43:13 +0200913 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100914
915 /* get ramdisk image data address and length */
916 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
917 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100918 show_boot_progress (-127);
Michal Simekb2474002008-07-11 10:43:13 +0200919 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100920 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100921 show_boot_progress (128);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100922
923 rd_data = (ulong)data;
924 rd_len = size;
925
926 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
927 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100928 show_boot_progress (-129);
Michal Simekb2474002008-07-11 10:43:13 +0200929 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100930 }
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +0100931 show_boot_progress (129);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100932
933 images->fit_hdr_rd = fit_hdr;
934 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100935 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100936 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100937#endif
938 default:
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100939 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowicza50d5152008-03-12 10:12:37 +0100940 rd_data = rd_len = rd_load = 0;
Kumar Galab8ca7342008-08-29 19:08:29 -0500941 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100942 }
Marian Balakowicz95418502008-01-31 13:55:39 +0100943
944#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100945 /*
946 * We need to copy the ramdisk to SRAM to let Linux boot
947 */
948 if (rd_data) {
949 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
950 rd_data = rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100951 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100952#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
Marian Balakowicz95418502008-01-31 13:55:39 +0100953
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100954 } else if (images->legacy_hdr_valid &&
Marian Balakowiczab00e022008-04-11 11:07:49 +0200955 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowicz95418502008-01-31 13:55:39 +0100956 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100957 * Now check if we have a legacy mult-component image,
958 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +0100959 */
960 show_boot_progress (13);
961 printf ("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100962 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100963 (ulong)images->legacy_hdr_os);
964
965 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz95418502008-01-31 13:55:39 +0100966 } else {
967 /*
968 * no initrd image
969 */
970 show_boot_progress (14);
971 rd_len = rd_data = 0;
972 }
973
974 if (!rd_data) {
975 debug ("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +0100976 } else {
977 *rd_start = rd_data;
978 *rd_end = rd_data + rd_len;
979 }
980 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
981 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600982
983 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +0100984}
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100985
Daniel Hellstrombb9c8292008-03-28 08:29:26 +0100986#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100987/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100988 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -0600989 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100990 * @rd_data: ramdisk data start address
991 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100992 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
993 * start address (after possible relocation)
994 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
995 * end address (after possible relocation)
996 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100997 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowicz9c701e82008-01-31 13:57:17 +0100998 * variable and if requested ramdisk data is moved to a specified location.
999 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001000 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1001 * start/end addresses if ramdisk image start and len were provided,
1002 * otherwise set initrd_start and initrd_end set to zeros.
1003 *
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001004 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001005 * 0 - success
1006 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001007 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001008int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001009 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001010{
1011 char *s;
1012 ulong initrd_high;
1013 int initrd_copy_to_ram = 1;
1014
1015 if ((s = getenv ("initrd_high")) != NULL) {
1016 /* a value of "no" or a similar string will act like 0,
1017 * turning the "load high" feature off. This is intentional.
1018 */
1019 initrd_high = simple_strtoul (s, NULL, 16);
1020 if (initrd_high == ~0)
1021 initrd_copy_to_ram = 0;
1022 } else {
1023 /* not set, no restrictions to load high */
1024 initrd_high = ~0;
1025 }
1026
Marian Balakowicz902ce702008-05-13 15:53:29 +02001027
1028#ifdef CONFIG_LOGBUFFER
1029 /* Prevent initrd from overwriting logbuffer */
1030 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1031#endif
1032
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001033 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1034 initrd_high, initrd_copy_to_ram);
1035
1036 if (rd_data) {
1037 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1038 debug (" in-place initrd\n");
1039 *initrd_start = rd_data;
1040 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001041 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001042 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001043 if (initrd_high)
Becky Bruced26d67c2008-06-09 20:37:18 -05001044 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001045 else
Becky Bruced26d67c2008-06-09 20:37:18 -05001046 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001047
Kumar Galab937bb72008-02-27 21:51:49 -06001048 if (*initrd_start == 0) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001049 puts ("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001050 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001051 }
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001052 show_boot_progress (12);
1053
1054 *initrd_end = *initrd_start + rd_len;
1055 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1056 *initrd_start, *initrd_end);
1057
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001058 memmove_wd ((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001059 (void *)rd_data, rd_len, CHUNKSZ);
1060
1061 puts ("OK\n");
1062 }
1063 } else {
1064 *initrd_start = 0;
1065 *initrd_end = 0;
1066 }
1067 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1068 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001069
Kumar Galab937bb72008-02-27 21:51:49 -06001070 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001071
Kumar Galab937bb72008-02-27 21:51:49 -06001072error:
1073 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001074}
Jean-Christophe PLAGNIOL-VILLARD0ba0a772008-11-24 13:33:51 +01001075#endif /* defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC) */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001076
Kumar Galaa5130ad2008-08-15 08:24:38 -05001077#ifdef CONFIG_OF_LIBFDT
1078static void fdt_error (const char *msg)
1079{
1080 puts ("ERROR: ");
1081 puts (msg);
1082 puts (" - must RESET the board to recover.\n");
1083}
1084
Wolfgang Denk3b506772009-08-19 11:42:56 +02001085static const image_header_t *image_get_fdt (ulong fdt_addr)
Kumar Galaa5130ad2008-08-15 08:24:38 -05001086{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001087 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001088
1089 image_print_contents (fdt_hdr);
1090
1091 puts (" Verifying Checksum ... ");
1092 if (!image_check_hcrc (fdt_hdr)) {
1093 fdt_error ("fdt header checksum invalid");
1094 return NULL;
1095 }
1096
1097 if (!image_check_dcrc (fdt_hdr)) {
1098 fdt_error ("fdt checksum invalid");
1099 return NULL;
1100 }
1101 puts ("OK\n");
1102
1103 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1104 fdt_error ("uImage is not a fdt");
1105 return NULL;
1106 }
1107 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1108 fdt_error ("uImage is compressed");
1109 return NULL;
1110 }
1111 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1112 fdt_error ("uImage data is not a fdt");
1113 return NULL;
1114 }
1115 return fdt_hdr;
1116}
1117
1118/**
1119 * fit_check_fdt - verify FIT format FDT subimage
1120 * @fit_hdr: pointer to the FIT header
1121 * fdt_noffset: FDT subimage node offset within FIT image
1122 * @verify: data CRC verification flag
1123 *
1124 * fit_check_fdt() verifies integrity of the FDT subimage and from
1125 * specified FIT image.
1126 *
1127 * returns:
1128 * 1, on success
1129 * 0, on failure
1130 */
1131#if defined(CONFIG_FIT)
1132static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1133{
1134 fit_image_print (fit, fdt_noffset, " ");
1135
1136 if (verify) {
1137 puts (" Verifying Hash Integrity ... ");
1138 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1139 fdt_error ("Bad Data Hash");
1140 return 0;
1141 }
1142 puts ("OK\n");
1143 }
1144
1145 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1146 fdt_error ("Not a FDT image");
1147 return 0;
1148 }
1149
1150 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1151 fdt_error ("FDT image is compressed");
1152 return 0;
1153 }
1154
1155 return 1;
1156}
1157#endif /* CONFIG_FIT */
1158
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001159#ifndef CONFIG_SYS_FDT_PAD
1160#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Galaa5130ad2008-08-15 08:24:38 -05001161#endif
1162
1163/**
1164 * boot_relocate_fdt - relocate flat device tree
1165 * @lmb: pointer to lmb handle, will be used for memory mgmt
1166 * @bootmap_base: base address of the bootmap region
1167 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1168 * @of_size: pointer to a ulong variable, will hold fdt length
1169 *
1170 * boot_relocate_fdt() determines if the of_flat_tree address is within
1171 * the bootmap and if not relocates it into that region
1172 *
1173 * of_flat_tree and of_size are set to final (after relocation) values
1174 *
1175 * returns:
1176 * 0 - success
1177 * 1 - failure
1178 */
1179int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1180 char **of_flat_tree, ulong *of_size)
1181{
1182 char *fdt_blob = *of_flat_tree;
1183 ulong relocate = 0;
1184 ulong of_len = 0;
1185
1186 /* nothing to do */
1187 if (*of_size == 0)
1188 return 0;
1189
1190 if (fdt_check_header (fdt_blob) != 0) {
1191 fdt_error ("image is not a fdt");
1192 goto error;
1193 }
1194
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001195#ifndef CONFIG_SYS_NO_FLASH
Kumar Galaa5130ad2008-08-15 08:24:38 -05001196 /* move the blob if it is in flash (set relocate) */
1197 if (addr2info ((ulong)fdt_blob) != NULL)
1198 relocate = 1;
1199#endif
1200
1201 /*
1202 * The blob needs to be inside the boot mapping.
1203 */
1204 if (fdt_blob < (char *)bootmap_base)
1205 relocate = 1;
1206
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001207 if ((fdt_blob + *of_size + CONFIG_SYS_FDT_PAD) >=
1208 ((char *)CONFIG_SYS_BOOTMAPSZ + bootmap_base))
Kumar Galaa5130ad2008-08-15 08:24:38 -05001209 relocate = 1;
1210
1211 /* move flattend device tree if needed */
1212 if (relocate) {
1213 int err;
1214 ulong of_start = 0;
1215
1216 /* position on a 4K boundary before the alloc_current */
1217 /* Pad the FDT by a specified amount */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001218 of_len = *of_size + CONFIG_SYS_FDT_PAD;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001219 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001220 (CONFIG_SYS_BOOTMAPSZ + bootmap_base));
Kumar Galaa5130ad2008-08-15 08:24:38 -05001221
1222 if (of_start == 0) {
1223 puts("device tree - allocation error\n");
1224 goto error;
1225 }
1226
1227 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1228 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1229 of_len, of_len);
1230
1231 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1232 of_start, of_start + of_len - 1);
1233
1234 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1235 if (err != 0) {
1236 fdt_error ("fdt move failed");
1237 goto error;
1238 }
1239 puts ("OK\n");
1240
1241 *of_flat_tree = (char *)of_start;
1242 *of_size = of_len;
1243 } else {
1244 *of_flat_tree = fdt_blob;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001245 of_len = (CONFIG_SYS_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001246 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1247 fdt_set_totalsize(*of_flat_tree, of_len);
1248
1249 *of_size = of_len;
1250 }
1251
Kumar Gala9f4e7e42008-08-15 08:24:39 -05001252 set_working_fdt_addr(*of_flat_tree);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001253 return 0;
1254
1255error:
1256 return 1;
1257}
1258
1259/**
1260 * boot_get_fdt - main fdt handling routine
1261 * @argc: command argument count
1262 * @argv: command argument list
1263 * @images: pointer to the bootm images structure
1264 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1265 * @of_size: pointer to a ulong variable, will hold fdt length
1266 *
1267 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1268 * Curently supported are the following ramdisk sources:
1269 * - multicomponent kernel/ramdisk image,
1270 * - commandline provided address of decicated ramdisk image.
1271 *
1272 * returns:
1273 * 0, if fdt image was found and valid, or skipped
1274 * of_flat_tree and of_size are set to fdt start address and length if
1275 * fdt image is found and valid
1276 *
1277 * 1, if fdt image is found but corrupted
1278 * of_flat_tree and of_size are set to 0 if no fdt exists
1279 */
1280int boot_get_fdt (int flag, int argc, char *argv[], bootm_headers_t *images,
1281 char **of_flat_tree, ulong *of_size)
1282{
Wolfgang Denk3b506772009-08-19 11:42:56 +02001283 const image_header_t *fdt_hdr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001284 ulong fdt_addr;
Kumar Galaa5130ad2008-08-15 08:24:38 -05001285 char *fdt_blob = NULL;
1286 ulong image_start, image_end;
1287 ulong load_start, load_end;
1288#if defined(CONFIG_FIT)
1289 void *fit_hdr;
1290 const char *fit_uname_config = NULL;
1291 const char *fit_uname_fdt = NULL;
1292 ulong default_addr;
1293 int cfg_noffset;
1294 int fdt_noffset;
1295 const void *data;
1296 size_t size;
1297#endif
1298
1299 *of_flat_tree = NULL;
1300 *of_size = 0;
1301
1302 if (argc > 3 || genimg_has_config (images)) {
1303#if defined(CONFIG_FIT)
1304 if (argc > 3) {
1305 /*
1306 * If the FDT blob comes from the FIT image and the
1307 * FIT image address is omitted in the command line
1308 * argument, try to use ramdisk or os FIT image
1309 * address or default load address.
1310 */
1311 if (images->fit_uname_rd)
1312 default_addr = (ulong)images->fit_hdr_rd;
1313 else if (images->fit_uname_os)
1314 default_addr = (ulong)images->fit_hdr_os;
1315 else
1316 default_addr = load_addr;
1317
1318 if (fit_parse_conf (argv[3], default_addr,
1319 &fdt_addr, &fit_uname_config)) {
1320 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1321 fit_uname_config, fdt_addr);
1322 } else if (fit_parse_subimage (argv[3], default_addr,
1323 &fdt_addr, &fit_uname_fdt)) {
1324 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1325 fit_uname_fdt, fdt_addr);
1326 } else
1327#endif
1328 {
1329 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1330 debug ("* fdt: cmdline image address = 0x%08lx\n",
1331 fdt_addr);
1332 }
1333#if defined(CONFIG_FIT)
1334 } else {
1335 /* use FIT configuration provided in first bootm
1336 * command argument
1337 */
1338 fdt_addr = (ulong)images->fit_hdr_os;
1339 fit_uname_config = images->fit_uname_cfg;
1340 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1341 fit_uname_config, fdt_addr);
1342
1343 /*
1344 * Check whether configuration has FDT blob defined,
1345 * if not quit silently.
1346 */
1347 fit_hdr = (void *)fdt_addr;
1348 cfg_noffset = fit_conf_get_node (fit_hdr,
1349 fit_uname_config);
1350 if (cfg_noffset < 0) {
1351 debug ("* fdt: no such config\n");
1352 return 0;
1353 }
1354
1355 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1356 cfg_noffset);
1357 if (fdt_noffset < 0) {
1358 debug ("* fdt: no fdt in config\n");
1359 return 0;
1360 }
1361 }
1362#endif
1363
1364 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1365 fdt_addr);
1366
1367 /* copy from dataflash if needed */
1368 fdt_addr = genimg_get_image (fdt_addr);
1369
1370 /*
1371 * Check if there is an FDT image at the
1372 * address provided in the second bootm argument
1373 * check image type, for FIT images get a FIT node.
1374 */
1375 switch (genimg_get_format ((void *)fdt_addr)) {
1376 case IMAGE_FORMAT_LEGACY:
1377 /* verify fdt_addr points to a valid image header */
1378 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1379 fdt_addr);
1380 fdt_hdr = image_get_fdt (fdt_addr);
1381 if (!fdt_hdr)
1382 goto error;
1383
1384 /*
1385 * move image data to the load address,
1386 * make sure we don't overwrite initial image
1387 */
1388 image_start = (ulong)fdt_hdr;
1389 image_end = image_get_image_end (fdt_hdr);
1390
1391 load_start = image_get_load (fdt_hdr);
1392 load_end = load_start + image_get_data_size (fdt_hdr);
1393
1394 if ((load_start < image_end) && (load_end > image_start)) {
1395 fdt_error ("fdt overwritten");
1396 goto error;
1397 }
1398
1399 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1400 image_get_data (fdt_hdr), load_start);
1401
1402 memmove ((void *)load_start,
1403 (void *)image_get_data (fdt_hdr),
1404 image_get_data_size (fdt_hdr));
1405
1406 fdt_blob = (char *)load_start;
1407 break;
1408 case IMAGE_FORMAT_FIT:
1409 /*
1410 * This case will catch both: new uImage format
1411 * (libfdt based) and raw FDT blob (also libfdt
1412 * based).
1413 */
1414#if defined(CONFIG_FIT)
1415 /* check FDT blob vs FIT blob */
1416 if (fit_check_format ((const void *)fdt_addr)) {
1417 /*
1418 * FIT image
1419 */
1420 fit_hdr = (void *)fdt_addr;
1421 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1422 fdt_addr);
1423
1424 if (!fit_uname_fdt) {
1425 /*
1426 * no FDT blob image node unit name,
1427 * try to get config node first. If
1428 * config unit node name is NULL
1429 * fit_conf_get_node() will try to
1430 * find default config node
1431 */
1432 cfg_noffset = fit_conf_get_node (fit_hdr,
1433 fit_uname_config);
1434
1435 if (cfg_noffset < 0) {
1436 fdt_error ("Could not find configuration node\n");
1437 goto error;
1438 }
1439
1440 fit_uname_config = fdt_get_name (fit_hdr,
1441 cfg_noffset, NULL);
1442 printf (" Using '%s' configuration\n",
1443 fit_uname_config);
1444
1445 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1446 cfg_noffset);
1447 fit_uname_fdt = fit_get_name (fit_hdr,
1448 fdt_noffset, NULL);
1449 } else {
1450 /* get FDT component image node offset */
1451 fdt_noffset = fit_image_get_node (fit_hdr,
1452 fit_uname_fdt);
1453 }
1454 if (fdt_noffset < 0) {
1455 fdt_error ("Could not find subimage node\n");
1456 goto error;
1457 }
1458
1459 printf (" Trying '%s' FDT blob subimage\n",
1460 fit_uname_fdt);
1461
1462 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1463 images->verify))
1464 goto error;
1465
1466 /* get ramdisk image data address and length */
1467 if (fit_image_get_data (fit_hdr, fdt_noffset,
1468 &data, &size)) {
1469 fdt_error ("Could not find FDT subimage data");
1470 goto error;
1471 }
1472
1473 /* verift that image data is a proper FDT blob */
1474 if (fdt_check_header ((char *)data) != 0) {
1475 fdt_error ("Subimage data is not a FTD");
1476 goto error;
1477 }
1478
1479 /*
1480 * move image data to the load address,
1481 * make sure we don't overwrite initial image
1482 */
1483 image_start = (ulong)fit_hdr;
1484 image_end = fit_get_end (fit_hdr);
1485
1486 if (fit_image_get_load (fit_hdr, fdt_noffset,
1487 &load_start) == 0) {
1488 load_end = load_start + size;
1489
1490 if ((load_start < image_end) &&
1491 (load_end > image_start)) {
1492 fdt_error ("FDT overwritten");
1493 goto error;
1494 }
1495
1496 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1497 (ulong)data, load_start);
1498
1499 memmove ((void *)load_start,
1500 (void *)data, size);
1501
1502 fdt_blob = (char *)load_start;
1503 } else {
1504 fdt_blob = (char *)data;
1505 }
1506
1507 images->fit_hdr_fdt = fit_hdr;
1508 images->fit_uname_fdt = fit_uname_fdt;
1509 images->fit_noffset_fdt = fdt_noffset;
1510 break;
1511 } else
1512#endif
1513 {
1514 /*
1515 * FDT blob
1516 */
1517 fdt_blob = (char *)fdt_addr;
1518 debug ("* fdt: raw FDT blob\n");
1519 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1520 }
1521 break;
1522 default:
Anatolij Gustschin128de002008-08-29 21:04:45 +02001523 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Galaa5130ad2008-08-15 08:24:38 -05001524 goto error;
1525 }
1526
1527 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1528
1529 } else if (images->legacy_hdr_valid &&
1530 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1531
1532 ulong fdt_data, fdt_len;
1533
1534 /*
1535 * Now check if we have a legacy multi-component image,
1536 * get second entry data start address and len.
1537 */
1538 printf ("## Flattened Device Tree from multi "
1539 "component Image at %08lX\n",
1540 (ulong)images->legacy_hdr_os);
1541
1542 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1543 if (fdt_len) {
1544
1545 fdt_blob = (char *)fdt_data;
1546 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1547
1548 if (fdt_check_header (fdt_blob) != 0) {
1549 fdt_error ("image is not a fdt");
1550 goto error;
1551 }
1552
1553 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1554 fdt_error ("fdt size != image size");
1555 goto error;
1556 }
1557 } else {
1558 debug ("## No Flattened Device Tree\n");
1559 return 0;
1560 }
1561 } else {
1562 debug ("## No Flattened Device Tree\n");
1563 return 0;
1564 }
1565
1566 *of_flat_tree = fdt_blob;
1567 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1568 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner7ddfafc2008-08-21 07:12:26 -07001569 (ulong)*of_flat_tree, *of_size);
Kumar Galaa5130ad2008-08-15 08:24:38 -05001570
1571 return 0;
1572
1573error:
1574 *of_flat_tree = 0;
1575 *of_size = 0;
1576 return 1;
1577}
1578#endif /* CONFIG_OF_LIBFDT */
1579
Jean-Christophe PLAGNIOL-VILLARD0ba0a772008-11-24 13:33:51 +01001580#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001581/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001582 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001583 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001584 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1585 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galab937bb72008-02-27 21:51:49 -06001586 * @bootmap_base: ulong variable, holds offset in physical memory to
1587 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001588 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001589 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galab937bb72008-02-27 21:51:49 -06001590 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001591 * variable is present its contents is copied to allocated kernel
1592 * command line.
1593 *
1594 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001595 * 0 - success
1596 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001597 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001598int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galab937bb72008-02-27 21:51:49 -06001599 ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001600{
1601 char *cmdline;
1602 char *s;
1603
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001604 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1605 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -06001606
1607 if (cmdline == NULL)
1608 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001609
1610 if ((s = getenv("bootargs")) == NULL)
1611 s = "";
1612
1613 strcpy(cmdline, s);
1614
1615 *cmd_start = (ulong) & cmdline[0];
1616 *cmd_end = *cmd_start + strlen(cmdline);
1617
1618 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1619
Kumar Galab937bb72008-02-27 21:51:49 -06001620 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001621}
1622
1623/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001624 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001625 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001626 * @kbd: double pointer to board info data
Kumar Galab937bb72008-02-27 21:51:49 -06001627 * @bootmap_base: ulong variable, holds offset in physical memory to
1628 * base of bootmap
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001629 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001630 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galab937bb72008-02-27 21:51:49 -06001631 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1632 * the current u-boot board info data.
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001633 *
1634 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001635 * 0 - success
1636 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001637 */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001638int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001639{
Becky Bruced26d67c2008-06-09 20:37:18 -05001640 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001641 CONFIG_SYS_BOOTMAPSZ + bootmap_base);
Kumar Galab937bb72008-02-27 21:51:49 -06001642 if (*kbd == NULL)
1643 return -1;
1644
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001645 **kbd = *(gd->bd);
1646
1647 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1648
1649#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1650 do_bdinfo(NULL, 0, 0, NULL);
1651#endif
1652
Kumar Galab937bb72008-02-27 21:51:49 -06001653 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001654}
1655#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001656#endif /* !USE_HOSTCC */
Marian Balakowicz95418502008-01-31 13:55:39 +01001657
Marian Balakowicz3413e222008-02-21 17:20:20 +01001658#if defined(CONFIG_FIT)
1659/*****************************************************************************/
1660/* New uImage format routines */
1661/*****************************************************************************/
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001662#ifndef USE_HOSTCC
Marian Balakowicz3413e222008-02-21 17:20:20 +01001663static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1664 ulong *addr, const char **name)
1665{
1666 const char *sep;
1667
1668 *addr = addr_curr;
1669 *name = NULL;
1670
1671 sep = strchr (spec, sepc);
1672 if (sep) {
1673 if (sep - spec > 0)
1674 *addr = simple_strtoul (spec, NULL, 16);
1675
1676 *name = sep + 1;
1677 return 1;
1678 }
1679
1680 return 0;
1681}
1682
1683/**
1684 * fit_parse_conf - parse FIT configuration spec
1685 * @spec: input string, containing configuration spec
1686 * @add_curr: current image address (to be used as a possible default)
1687 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1688 * configuration
1689 * @conf_name double pointer to a char, will hold pointer to a configuration
1690 * unit name
1691 *
1692 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1693 * where <addr> is a FIT image address that contains configuration
1694 * with a <conf> unit name.
1695 *
1696 * Address part is optional, and if omitted default add_curr will
1697 * be used instead.
1698 *
1699 * returns:
1700 * 1 if spec is a valid configuration string,
1701 * addr and conf_name are set accordingly
1702 * 0 otherwise
1703 */
1704inline int fit_parse_conf (const char *spec, ulong addr_curr,
1705 ulong *addr, const char **conf_name)
1706{
1707 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1708}
1709
1710/**
1711 * fit_parse_subimage - parse FIT subimage spec
1712 * @spec: input string, containing subimage spec
1713 * @add_curr: current image address (to be used as a possible default)
1714 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1715 * subimage
1716 * @image_name: double pointer to a char, will hold pointer to a subimage name
1717 *
1718 * fit_parse_subimage() expects subimage spec in the for of
1719 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1720 * subimage with a <subimg> unit name.
1721 *
1722 * Address part is optional, and if omitted default add_curr will
1723 * be used instead.
1724 *
1725 * returns:
1726 * 1 if spec is a valid subimage string,
1727 * addr and image_name are set accordingly
1728 * 0 otherwise
1729 */
1730inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1731 ulong *addr, const char **image_name)
1732{
1733 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1734}
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001735#endif /* !USE_HOSTCC */
1736
1737static void fit_get_debug (const void *fit, int noffset,
1738 char *prop_name, int err)
1739{
1740 debug ("Can't get '%s' property from FIT 0x%08lx, "
1741 "node: offset %d, name %s (%s)\n",
1742 prop_name, (ulong)fit, noffset,
1743 fit_get_name (fit, noffset, NULL),
1744 fdt_strerror (err));
1745}
1746
1747/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001748 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001749 * @fit: pointer to the FIT format image header
1750 * @p: pointer to prefix string
1751 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001752 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001753 * The routine prints out FIT image properties (root node level) follwed by
1754 * the details of each component image.
1755 *
1756 * returns:
1757 * no returned results
1758 */
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001759void fit_print_contents (const void *fit)
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001760{
1761 char *desc;
1762 char *uname;
1763 int images_noffset;
1764 int confs_noffset;
1765 int noffset;
1766 int ndepth;
1767 int count = 0;
1768 int ret;
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001769 const char *p;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001770#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1771 time_t timestamp;
1772#endif
1773
Bartlomiej Sieka47868592008-04-18 12:39:23 +02001774#ifdef USE_HOSTCC
1775 p = "";
1776#else
1777 p = " ";
1778#endif
1779
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001780 /* Root node properties */
1781 ret = fit_get_desc (fit, 0, &desc);
1782 printf ("%sFIT description: ", p);
1783 if (ret)
1784 printf ("unavailable\n");
1785 else
1786 printf ("%s\n", desc);
1787
1788#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1789 ret = fit_get_timestamp (fit, 0, &timestamp);
1790 printf ("%sCreated: ", p);
1791 if (ret)
1792 printf ("unavailable\n");
1793 else
1794 genimg_print_time (timestamp);
1795#endif
1796
1797 /* Find images parent node offset */
1798 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1799 if (images_noffset < 0) {
1800 printf ("Can't find images parent node '%s' (%s)\n",
1801 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1802 return;
1803 }
1804
1805 /* Process its subnodes, print out component images details */
1806 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1807 (noffset >= 0) && (ndepth > 0);
1808 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1809 if (ndepth == 1) {
1810 /*
1811 * Direct child node of the images parent node,
1812 * i.e. component image node.
1813 */
1814 printf ("%s Image %u (%s)\n", p, count++,
1815 fit_get_name(fit, noffset, NULL));
1816
1817 fit_image_print (fit, noffset, p);
1818 }
1819 }
1820
1821 /* Find configurations parent node offset */
1822 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1823 if (confs_noffset < 0) {
1824 debug ("Can't get configurations parent node '%s' (%s)\n",
1825 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1826 return;
1827 }
1828
1829 /* get default configuration unit name from default property */
1830 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1831 if (uname)
1832 printf ("%s Default Configuration: '%s'\n", p, uname);
1833
1834 /* Process its subnodes, print out configurations details */
1835 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1836 (noffset >= 0) && (ndepth > 0);
1837 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1838 if (ndepth == 1) {
1839 /*
1840 * Direct child node of the configurations parent node,
1841 * i.e. configuration node.
1842 */
1843 printf ("%s Configuration %u (%s)\n", p, count++,
1844 fit_get_name(fit, noffset, NULL));
1845
1846 fit_conf_print (fit, noffset, p);
1847 }
1848 }
1849}
1850
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001851/**
1852 * fit_image_print - prints out the FIT component image details
1853 * @fit: pointer to the FIT format image header
1854 * @image_noffset: offset of the component image node
1855 * @p: pointer to prefix string
1856 *
1857 * fit_image_print() lists all mandatory properies for the processed component
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001858 * image. If present, hash nodes are printed out as well. Load
1859 * address for images of type firmware is also printed out. Since the load
1860 * address is not mandatory for firmware images, it will be output as
1861 * "unavailable" when not present.
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001862 *
1863 * returns:
1864 * no returned results
1865 */
1866void fit_image_print (const void *fit, int image_noffset, const char *p)
1867{
1868 char *desc;
1869 uint8_t type, arch, os, comp;
1870 size_t size;
1871 ulong load, entry;
1872 const void *data;
1873 int noffset;
1874 int ndepth;
1875 int ret;
1876
1877 /* Mandatory properties */
1878 ret = fit_get_desc (fit, image_noffset, &desc);
1879 printf ("%s Description: ", p);
1880 if (ret)
1881 printf ("unavailable\n");
1882 else
1883 printf ("%s\n", desc);
1884
1885 fit_image_get_type (fit, image_noffset, &type);
1886 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1887
1888 fit_image_get_comp (fit, image_noffset, &comp);
1889 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1890
1891 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1892
1893#ifndef USE_HOSTCC
1894 printf ("%s Data Start: ", p);
1895 if (ret)
1896 printf ("unavailable\n");
1897 else
1898 printf ("0x%08lx\n", (ulong)data);
1899#endif
1900
1901 printf ("%s Data Size: ", p);
1902 if (ret)
1903 printf ("unavailable\n");
1904 else
1905 genimg_print_size (size);
1906
1907 /* Remaining, type dependent properties */
1908 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1909 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1910 (type == IH_TYPE_FLATDT)) {
1911 fit_image_get_arch (fit, image_noffset, &arch);
1912 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1913 }
1914
1915 if (type == IH_TYPE_KERNEL) {
1916 fit_image_get_os (fit, image_noffset, &os);
1917 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1918 }
1919
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001920 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1921 (type == IH_TYPE_FIRMWARE)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001922 ret = fit_image_get_load (fit, image_noffset, &load);
1923 printf ("%s Load Address: ", p);
1924 if (ret)
1925 printf ("unavailable\n");
1926 else
1927 printf ("0x%08lx\n", load);
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001928 }
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001929
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02001930 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
Marian Balakowicze227fbb2008-02-29 21:24:06 +01001931 fit_image_get_entry (fit, image_noffset, &entry);
1932 printf ("%s Entry Point: ", p);
1933 if (ret)
1934 printf ("unavailable\n");
1935 else
1936 printf ("0x%08lx\n", entry);
1937 }
1938
1939 /* Process all hash subnodes of the component image node */
1940 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1941 (noffset >= 0) && (ndepth > 0);
1942 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1943 if (ndepth == 1) {
1944 /* Direct child node of the component image node */
1945 fit_image_print_hash (fit, noffset, p);
1946 }
1947 }
1948}
1949
1950/**
1951 * fit_image_print_hash - prints out the hash node details
1952 * @fit: pointer to the FIT format image header
1953 * @noffset: offset of the hash node
1954 * @p: pointer to prefix string
1955 *
1956 * fit_image_print_hash() lists properies for the processed hash node
1957 *
1958 * returns:
1959 * no returned results
1960 */
1961void fit_image_print_hash (const void *fit, int noffset, const char *p)
1962{
1963 char *algo;
1964 uint8_t *value;
1965 int value_len;
1966 int i, ret;
1967
1968 /*
1969 * Check subnode name, must be equal to "hash".
1970 * Multiple hash nodes require unique unit node
1971 * names, e.g. hash@1, hash@2, etc.
1972 */
1973 if (strncmp (fit_get_name(fit, noffset, NULL),
1974 FIT_HASH_NODENAME,
1975 strlen(FIT_HASH_NODENAME)) != 0)
1976 return;
1977
1978 debug ("%s Hash node: '%s'\n", p,
1979 fit_get_name (fit, noffset, NULL));
1980
1981 printf ("%s Hash algo: ", p);
1982 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1983 printf ("invalid/unsupported\n");
1984 return;
1985 }
1986 printf ("%s\n", algo);
1987
1988 ret = fit_image_hash_get_value (fit, noffset, &value,
1989 &value_len);
1990 printf ("%s Hash value: ", p);
1991 if (ret) {
1992 printf ("unavailable\n");
1993 } else {
1994 for (i = 0; i < value_len; i++)
1995 printf ("%02x", value[i]);
1996 printf ("\n");
1997 }
1998
1999 debug ("%s Hash len: %d\n", p, value_len);
2000}
2001
2002/**
2003 * fit_get_desc - get node description property
2004 * @fit: pointer to the FIT format image header
2005 * @noffset: node offset
2006 * @desc: double pointer to the char, will hold pointer to the descrption
2007 *
2008 * fit_get_desc() reads description property from a given node, if
2009 * description is found pointer to it is returened in third call argument.
2010 *
2011 * returns:
2012 * 0, on success
2013 * -1, on failure
2014 */
2015int fit_get_desc (const void *fit, int noffset, char **desc)
2016{
2017 int len;
2018
2019 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2020 if (*desc == NULL) {
2021 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2022 return -1;
2023 }
2024
2025 return 0;
2026}
2027
2028/**
2029 * fit_get_timestamp - get node timestamp property
2030 * @fit: pointer to the FIT format image header
2031 * @noffset: node offset
2032 * @timestamp: pointer to the time_t, will hold read timestamp
2033 *
2034 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2035 * is found and has a correct size its value is retured in third call
2036 * argument.
2037 *
2038 * returns:
2039 * 0, on success
2040 * -1, on property read failure
2041 * -2, on wrong timestamp size
2042 */
2043int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2044{
2045 int len;
2046 const void *data;
2047
2048 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2049 if (data == NULL) {
2050 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2051 return -1;
2052 }
2053 if (len != sizeof (uint32_t)) {
2054 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2055 return -2;
2056 }
2057
2058 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2059 return 0;
2060}
2061
2062/**
2063 * fit_image_get_node - get node offset for component image of a given unit name
2064 * @fit: pointer to the FIT format image header
2065 * @image_uname: component image node unit name
2066 *
2067 * fit_image_get_node() finds a component image (withing the '/images'
2068 * node) of a provided unit name. If image is found its node offset is
2069 * returned to the caller.
2070 *
2071 * returns:
2072 * image node offset when found (>=0)
2073 * negative number on failure (FDT_ERR_* code)
2074 */
2075int fit_image_get_node (const void *fit, const char *image_uname)
2076{
2077 int noffset, images_noffset;
2078
2079 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2080 if (images_noffset < 0) {
2081 debug ("Can't find images parent node '%s' (%s)\n",
2082 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2083 return images_noffset;
2084 }
2085
2086 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2087 if (noffset < 0) {
2088 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2089 image_uname, fdt_strerror (noffset));
2090 }
2091
2092 return noffset;
2093}
2094
2095/**
2096 * fit_image_get_os - get os id for a given component image node
2097 * @fit: pointer to the FIT format image header
2098 * @noffset: component image node offset
2099 * @os: pointer to the uint8_t, will hold os numeric id
2100 *
2101 * fit_image_get_os() finds os property in a given component image node.
2102 * If the property is found, its (string) value is translated to the numeric
2103 * id which is returned to the caller.
2104 *
2105 * returns:
2106 * 0, on success
2107 * -1, on failure
2108 */
2109int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2110{
2111 int len;
2112 const void *data;
2113
2114 /* Get OS name from property data */
2115 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2116 if (data == NULL) {
2117 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2118 *os = -1;
2119 return -1;
2120 }
2121
2122 /* Translate OS name to id */
2123 *os = genimg_get_os_id (data);
2124 return 0;
2125}
2126
2127/**
2128 * fit_image_get_arch - get arch id for a given component image node
2129 * @fit: pointer to the FIT format image header
2130 * @noffset: component image node offset
2131 * @arch: pointer to the uint8_t, will hold arch numeric id
2132 *
2133 * fit_image_get_arch() finds arch property in a given component image node.
2134 * If the property is found, its (string) value is translated to the numeric
2135 * id which is returned to the caller.
2136 *
2137 * returns:
2138 * 0, on success
2139 * -1, on failure
2140 */
2141int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2142{
2143 int len;
2144 const void *data;
2145
2146 /* Get architecture name from property data */
2147 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2148 if (data == NULL) {
2149 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2150 *arch = -1;
2151 return -1;
2152 }
2153
2154 /* Translate architecture name to id */
2155 *arch = genimg_get_arch_id (data);
2156 return 0;
2157}
2158
2159/**
2160 * fit_image_get_type - get type id for a given component image node
2161 * @fit: pointer to the FIT format image header
2162 * @noffset: component image node offset
2163 * @type: pointer to the uint8_t, will hold type numeric id
2164 *
2165 * fit_image_get_type() finds type property in a given component image node.
2166 * If the property is found, its (string) value is translated to the numeric
2167 * id which is returned to the caller.
2168 *
2169 * returns:
2170 * 0, on success
2171 * -1, on failure
2172 */
2173int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2174{
2175 int len;
2176 const void *data;
2177
2178 /* Get image type name from property data */
2179 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2180 if (data == NULL) {
2181 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2182 *type = -1;
2183 return -1;
2184 }
2185
2186 /* Translate image type name to id */
2187 *type = genimg_get_type_id (data);
2188 return 0;
2189}
2190
2191/**
2192 * fit_image_get_comp - get comp id for a given component image node
2193 * @fit: pointer to the FIT format image header
2194 * @noffset: component image node offset
2195 * @comp: pointer to the uint8_t, will hold comp numeric id
2196 *
2197 * fit_image_get_comp() finds comp property in a given component image node.
2198 * If the property is found, its (string) value is translated to the numeric
2199 * id which is returned to the caller.
2200 *
2201 * returns:
2202 * 0, on success
2203 * -1, on failure
2204 */
2205int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2206{
2207 int len;
2208 const void *data;
2209
2210 /* Get compression name from property data */
2211 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2212 if (data == NULL) {
2213 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2214 *comp = -1;
2215 return -1;
2216 }
2217
2218 /* Translate compression name to id */
2219 *comp = genimg_get_comp_id (data);
2220 return 0;
2221}
2222
2223/**
2224 * fit_image_get_load - get load address property for a given component image node
2225 * @fit: pointer to the FIT format image header
2226 * @noffset: component image node offset
2227 * @load: pointer to the uint32_t, will hold load address
2228 *
2229 * fit_image_get_load() finds load address property in a given component image node.
2230 * If the property is found, its value is returned to the caller.
2231 *
2232 * returns:
2233 * 0, on success
2234 * -1, on failure
2235 */
2236int fit_image_get_load (const void *fit, int noffset, ulong *load)
2237{
2238 int len;
2239 const uint32_t *data;
2240
2241 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2242 if (data == NULL) {
2243 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2244 return -1;
2245 }
2246
2247 *load = uimage_to_cpu (*data);
2248 return 0;
2249}
2250
2251/**
2252 * fit_image_get_entry - get entry point address property for a given component image node
2253 * @fit: pointer to the FIT format image header
2254 * @noffset: component image node offset
2255 * @entry: pointer to the uint32_t, will hold entry point address
2256 *
2257 * fit_image_get_entry() finds entry point address property in a given component image node.
2258 * If the property is found, its value is returned to the caller.
2259 *
2260 * returns:
2261 * 0, on success
2262 * -1, on failure
2263 */
2264int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2265{
2266 int len;
2267 const uint32_t *data;
2268
2269 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2270 if (data == NULL) {
2271 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2272 return -1;
2273 }
2274
2275 *entry = uimage_to_cpu (*data);
2276 return 0;
2277}
2278
2279/**
2280 * fit_image_get_data - get data property and its size for a given component image node
2281 * @fit: pointer to the FIT format image header
2282 * @noffset: component image node offset
2283 * @data: double pointer to void, will hold data property's data address
2284 * @size: pointer to size_t, will hold data property's data size
2285 *
2286 * fit_image_get_data() finds data property in a given component image node.
2287 * If the property is found its data start address and size are returned to
2288 * the caller.
2289 *
2290 * returns:
2291 * 0, on success
2292 * -1, on failure
2293 */
2294int fit_image_get_data (const void *fit, int noffset,
2295 const void **data, size_t *size)
2296{
2297 int len;
2298
2299 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2300 if (*data == NULL) {
2301 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2302 *size = 0;
2303 return -1;
2304 }
2305
2306 *size = len;
2307 return 0;
2308}
2309
2310/**
2311 * fit_image_hash_get_algo - get hash algorithm name
2312 * @fit: pointer to the FIT format image header
2313 * @noffset: hash node offset
2314 * @algo: double pointer to char, will hold pointer to the algorithm name
2315 *
2316 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2317 * If the property is found its data start address is returned to the caller.
2318 *
2319 * returns:
2320 * 0, on success
2321 * -1, on failure
2322 */
2323int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2324{
2325 int len;
2326
2327 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2328 if (*algo == NULL) {
2329 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2330 return -1;
2331 }
2332
2333 return 0;
2334}
2335
2336/**
2337 * fit_image_hash_get_value - get hash value and length
2338 * @fit: pointer to the FIT format image header
2339 * @noffset: hash node offset
2340 * @value: double pointer to uint8_t, will hold address of a hash value data
2341 * @value_len: pointer to an int, will hold hash data length
2342 *
2343 * fit_image_hash_get_value() finds hash value property in a given hash node.
2344 * If the property is found its data start address and size are returned to
2345 * the caller.
2346 *
2347 * returns:
2348 * 0, on success
2349 * -1, on failure
2350 */
2351int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2352 int *value_len)
2353{
2354 int len;
2355
2356 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2357 if (*value == NULL) {
2358 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2359 *value_len = 0;
2360 return -1;
2361 }
2362
2363 *value_len = len;
2364 return 0;
2365}
2366
2367/**
2368 * fit_set_timestamp - set node timestamp property
2369 * @fit: pointer to the FIT format image header
2370 * @noffset: node offset
2371 * @timestamp: timestamp value to be set
2372 *
2373 * fit_set_timestamp() attempts to set timestamp property in the requested
2374 * node and returns operation status to the caller.
2375 *
2376 * returns:
2377 * 0, on success
2378 * -1, on property read failure
2379 */
2380int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2381{
2382 uint32_t t;
2383 int ret;
2384
2385 t = cpu_to_uimage (timestamp);
2386 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2387 sizeof (uint32_t));
2388 if (ret) {
2389 printf ("Can't set '%s' property for '%s' node (%s)\n",
2390 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2391 fdt_strerror (ret));
2392 return -1;
2393 }
2394
2395 return 0;
2396}
2397
2398/**
2399 * calculate_hash - calculate and return hash for provided input data
2400 * @data: pointer to the input data
2401 * @data_len: data length
2402 * @algo: requested hash algorithm
2403 * @value: pointer to the char, will hold hash value data (caller must
2404 * allocate enough free space)
2405 * value_len: length of the calculated hash
2406 *
2407 * calculate_hash() computes input data hash according to the requested algorithm.
2408 * Resulting hash value is placed in caller provided 'value' buffer, length
2409 * of the calculated hash is returned via value_len pointer argument.
2410 *
2411 * returns:
2412 * 0, on success
2413 * -1, when algo is unsupported
2414 */
2415static int calculate_hash (const void *data, int data_len, const char *algo,
2416 uint8_t *value, int *value_len)
2417{
2418 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002419 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2420 CHUNKSZ_CRC32);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002421 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2422 *value_len = 4;
2423 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002424 sha1_csum_wd ((unsigned char *) data, data_len,
2425 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002426 *value_len = 20;
2427 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka222e2ca2008-04-25 13:54:02 +02002428 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka2e6f2852008-03-14 16:22:34 +01002429 *value_len = 16;
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002430 } else {
2431 debug ("Unsupported hash alogrithm\n");
2432 return -1;
2433 }
2434 return 0;
2435}
2436
2437#ifdef USE_HOSTCC
2438/**
2439 * fit_set_hashes - process FIT component image nodes and calculate hashes
2440 * @fit: pointer to the FIT format image header
2441 *
2442 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2443 * Hashes are calculated for all component images which have hash subnodes
2444 * with algorithm property set to one of the supported hash algorithms.
2445 *
2446 * returns
2447 * 0, on success
2448 * libfdt error code, on failure
2449 */
2450int fit_set_hashes (void *fit)
2451{
2452 int images_noffset;
2453 int noffset;
2454 int ndepth;
2455 int ret;
2456
2457 /* Find images parent node offset */
2458 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2459 if (images_noffset < 0) {
2460 printf ("Can't find images parent node '%s' (%s)\n",
2461 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2462 return images_noffset;
2463 }
2464
2465 /* Process its subnodes, print out component images details */
2466 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2467 (noffset >= 0) && (ndepth > 0);
2468 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2469 if (ndepth == 1) {
2470 /*
2471 * Direct child node of the images parent node,
2472 * i.e. component image node.
2473 */
2474 ret = fit_image_set_hashes (fit, noffset);
2475 if (ret)
2476 return ret;
2477 }
2478 }
2479
2480 return 0;
2481}
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01002482
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002483/**
2484 * fit_image_set_hashes - calculate/set hashes for given component image node
2485 * @fit: pointer to the FIT format image header
2486 * @image_noffset: requested component image node
2487 *
2488 * fit_image_set_hashes() adds hash values for an component image node. All
2489 * existing hash subnodes are checked, if algorithm property is set to one of
2490 * the supported hash algorithms, hash value is computed and corresponding
2491 * hash node property is set, for example:
2492 *
2493 * Input component image node structure:
2494 *
2495 * o image@1 (at image_noffset)
2496 * | - data = [binary data]
2497 * o hash@1
2498 * |- algo = "sha1"
2499 *
2500 * Output component image node structure:
2501 *
2502 * o image@1 (at image_noffset)
2503 * | - data = [binary data]
2504 * o hash@1
2505 * |- algo = "sha1"
2506 * |- value = sha1(data)
2507 *
2508 * returns:
2509 * 0 on sucess
2510 * <0 on failure
2511 */
2512int fit_image_set_hashes (void *fit, int image_noffset)
2513{
2514 const void *data;
2515 size_t size;
2516 char *algo;
2517 uint8_t value[FIT_MAX_HASH_LEN];
2518 int value_len;
2519 int noffset;
2520 int ndepth;
2521
2522 /* Get image data and data length */
2523 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2524 printf ("Can't get image data/size\n");
2525 return -1;
2526 }
2527
2528 /* Process all hash subnodes of the component image node */
2529 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2530 (noffset >= 0) && (ndepth > 0);
2531 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2532 if (ndepth == 1) {
2533 /* Direct child node of the component image node */
2534
2535 /*
2536 * Check subnode name, must be equal to "hash".
2537 * Multiple hash nodes require unique unit node
2538 * names, e.g. hash@1, hash@2, etc.
2539 */
2540 if (strncmp (fit_get_name(fit, noffset, NULL),
2541 FIT_HASH_NODENAME,
2542 strlen(FIT_HASH_NODENAME)) != 0) {
2543 /* Not a hash subnode, skip it */
2544 continue;
2545 }
2546
2547 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2548 printf ("Can't get hash algo property for "
2549 "'%s' hash node in '%s' image node\n",
2550 fit_get_name (fit, noffset, NULL),
2551 fit_get_name (fit, image_noffset, NULL));
2552 return -1;
2553 }
2554
2555 if (calculate_hash (data, size, algo, value, &value_len)) {
2556 printf ("Unsupported hash algorithm (%s) for "
2557 "'%s' hash node in '%s' image node\n",
2558 algo, fit_get_name (fit, noffset, NULL),
2559 fit_get_name (fit, image_noffset, NULL));
2560 return -1;
2561 }
2562
2563 if (fit_image_hash_set_value (fit, noffset, value,
2564 value_len)) {
2565 printf ("Can't set hash value for "
2566 "'%s' hash node in '%s' image node\n",
2567 fit_get_name (fit, noffset, NULL),
2568 fit_get_name (fit, image_noffset, NULL));
2569 return -1;
2570 }
2571 }
2572 }
2573
2574 return 0;
2575}
2576
2577/**
2578 * fit_image_hash_set_value - set hash value in requested has node
2579 * @fit: pointer to the FIT format image header
2580 * @noffset: hash node offset
2581 * @value: hash value to be set
2582 * @value_len: hash value length
2583 *
2584 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2585 * given and returns operation status to the caller.
2586 *
2587 * returns
2588 * 0, on success
2589 * -1, on failure
2590 */
2591int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2592 int value_len)
2593{
2594 int ret;
2595
2596 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2597 if (ret) {
2598 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2599 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2600 fdt_strerror (ret));
2601 return -1;
2602 }
2603
2604 return 0;
2605}
2606#endif /* USE_HOSTCC */
2607
2608/**
2609 * fit_image_check_hashes - verify data intergity
2610 * @fit: pointer to the FIT format image header
2611 * @image_noffset: component image node offset
2612 *
2613 * fit_image_check_hashes() goes over component image hash nodes,
2614 * re-calculates each data hash and compares with the value stored in hash
2615 * node.
2616 *
2617 * returns:
2618 * 1, if all hashes are valid
2619 * 0, otherwise (or on error)
2620 */
2621int fit_image_check_hashes (const void *fit, int image_noffset)
2622{
2623 const void *data;
2624 size_t size;
2625 char *algo;
2626 uint8_t *fit_value;
2627 int fit_value_len;
2628 uint8_t value[FIT_MAX_HASH_LEN];
2629 int value_len;
2630 int noffset;
2631 int ndepth;
2632 char *err_msg = "";
2633
2634 /* Get image data and data length */
2635 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2636 printf ("Can't get image data/size\n");
2637 return 0;
2638 }
2639
2640 /* Process all hash subnodes of the component image node */
2641 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2642 (noffset >= 0) && (ndepth > 0);
2643 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2644 if (ndepth == 1) {
2645 /* Direct child node of the component image node */
2646
2647 /*
2648 * Check subnode name, must be equal to "hash".
2649 * Multiple hash nodes require unique unit node
2650 * names, e.g. hash@1, hash@2, etc.
2651 */
2652 if (strncmp (fit_get_name(fit, noffset, NULL),
2653 FIT_HASH_NODENAME,
2654 strlen(FIT_HASH_NODENAME)) != 0)
2655 continue;
2656
2657 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002658 err_msg = " error!\nCan't get hash algo "
2659 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002660 goto error;
2661 }
2662 printf ("%s", algo);
2663
2664 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2665 &fit_value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002666 err_msg = " error!\nCan't get hash value "
2667 "property";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002668 goto error;
2669 }
2670
2671 if (calculate_hash (data, size, algo, value, &value_len)) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002672 err_msg = " error!\nUnsupported hash algorithm";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002673 goto error;
2674 }
2675
2676 if (value_len != fit_value_len) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002677 err_msg = " error !\nBad hash value len";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002678 goto error;
2679 } else if (memcmp (value, fit_value, value_len) != 0) {
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002680 err_msg = " error!\nBad hash value";
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002681 goto error;
2682 }
2683 printf ("+ ");
2684 }
2685 }
2686
2687 return 1;
2688
2689error:
2690 printf ("%s for '%s' hash node in '%s' image node\n",
2691 err_msg, fit_get_name (fit, noffset, NULL),
2692 fit_get_name (fit, image_noffset, NULL));
2693 return 0;
2694}
2695
2696/**
Bartlomiej Sieka1557a542008-09-09 12:58:15 +02002697 * fit_all_image_check_hashes - verify data intergity for all images
2698 * @fit: pointer to the FIT format image header
2699 *
2700 * fit_all_image_check_hashes() goes over all images in the FIT and
2701 * for every images checks if all it's hashes are valid.
2702 *
2703 * returns:
2704 * 1, if all hashes of all images are valid
2705 * 0, otherwise (or on error)
2706 */
2707int fit_all_image_check_hashes (const void *fit)
2708{
2709 int images_noffset;
2710 int noffset;
2711 int ndepth;
2712 int count;
2713
2714 /* Find images parent node offset */
2715 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2716 if (images_noffset < 0) {
2717 printf ("Can't find images parent node '%s' (%s)\n",
2718 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2719 return 0;
2720 }
2721
2722 /* Process all image subnodes, check hashes for each */
2723 printf ("## Checking hash(es) for FIT Image at %08lx ...\n",
2724 (ulong)fit);
2725 for (ndepth = 0, count = 0,
2726 noffset = fdt_next_node (fit, images_noffset, &ndepth);
2727 (noffset >= 0) && (ndepth > 0);
2728 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2729 if (ndepth == 1) {
2730 /*
2731 * Direct child node of the images parent node,
2732 * i.e. component image node.
2733 */
2734 printf (" Hash(es) for Image %u (%s): ", count++,
2735 fit_get_name (fit, noffset, NULL));
2736
2737 if (!fit_image_check_hashes (fit, noffset))
2738 return 0;
2739 printf ("\n");
2740 }
2741 }
2742 return 1;
2743}
2744
2745/**
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002746 * fit_image_check_os - check whether image node is of a given os type
2747 * @fit: pointer to the FIT format image header
2748 * @noffset: component image node offset
2749 * @os: requested image os
2750 *
2751 * fit_image_check_os() reads image os property and compares its numeric
2752 * id with the requested os. Comparison result is returned to the caller.
2753 *
2754 * returns:
2755 * 1 if image is of given os type
2756 * 0 otherwise (or on error)
2757 */
2758int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2759{
2760 uint8_t image_os;
2761
2762 if (fit_image_get_os (fit, noffset, &image_os))
2763 return 0;
2764 return (os == image_os);
2765}
2766
2767/**
2768 * fit_image_check_arch - check whether image node is of a given arch
2769 * @fit: pointer to the FIT format image header
2770 * @noffset: component image node offset
2771 * @arch: requested imagearch
2772 *
2773 * fit_image_check_arch() reads image arch property and compares its numeric
2774 * id with the requested arch. Comparison result is returned to the caller.
2775 *
2776 * returns:
2777 * 1 if image is of given arch
2778 * 0 otherwise (or on error)
2779 */
2780int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2781{
2782 uint8_t image_arch;
2783
2784 if (fit_image_get_arch (fit, noffset, &image_arch))
2785 return 0;
2786 return (arch == image_arch);
2787}
2788
2789/**
2790 * fit_image_check_type - check whether image node is of a given type
2791 * @fit: pointer to the FIT format image header
2792 * @noffset: component image node offset
2793 * @type: requested image type
2794 *
2795 * fit_image_check_type() reads image type property and compares its numeric
2796 * id with the requested type. Comparison result is returned to the caller.
2797 *
2798 * returns:
2799 * 1 if image is of given type
2800 * 0 otherwise (or on error)
2801 */
2802int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2803{
2804 uint8_t image_type;
2805
2806 if (fit_image_get_type (fit, noffset, &image_type))
2807 return 0;
2808 return (type == image_type);
2809}
2810
2811/**
2812 * fit_image_check_comp - check whether image node uses given compression
2813 * @fit: pointer to the FIT format image header
2814 * @noffset: component image node offset
2815 * @comp: requested image compression type
2816 *
2817 * fit_image_check_comp() reads image compression property and compares its
2818 * numeric id with the requested compression type. Comparison result is
2819 * returned to the caller.
2820 *
2821 * returns:
2822 * 1 if image uses requested compression
2823 * 0 otherwise (or on error)
2824 */
2825int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2826{
2827 uint8_t image_comp;
2828
2829 if (fit_image_get_comp (fit, noffset, &image_comp))
2830 return 0;
2831 return (comp == image_comp);
2832}
2833
2834/**
2835 * fit_check_format - sanity check FIT image format
2836 * @fit: pointer to the FIT format image header
2837 *
2838 * fit_check_format() runs a basic sanity FIT image verification.
2839 * Routine checks for mandatory properties, nodes, etc.
2840 *
2841 * returns:
2842 * 1, on success
2843 * 0, on failure
2844 */
2845int fit_check_format (const void *fit)
2846{
2847 /* mandatory / node 'description' property */
2848 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2849 debug ("Wrong FIT format: no description\n");
2850 return 0;
2851 }
2852
2853#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2854 /* mandatory / node 'timestamp' property */
2855 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
Bartlomiej Sieka0cc0b212008-10-01 15:26:32 +02002856 debug ("Wrong FIT format: no timestamp\n");
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002857 return 0;
2858 }
2859#endif
2860
2861 /* mandatory subimages parent '/images' node */
2862 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2863 debug ("Wrong FIT format: no images parent node\n");
2864 return 0;
2865 }
2866
2867 return 1;
2868}
2869
2870/**
2871 * fit_conf_get_node - get node offset for configuration of a given unit name
2872 * @fit: pointer to the FIT format image header
2873 * @conf_uname: configuration node unit name
2874 *
2875 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2876 * parant node) of a provided unit name. If configuration is found its node offset
2877 * is returned to the caller.
2878 *
2879 * When NULL is provided in second argument fit_conf_get_node() will search
2880 * for a default configuration node instead. Default configuration node unit name
2881 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2882 *
2883 * returns:
2884 * configuration node offset when found (>=0)
2885 * negative number on failure (FDT_ERR_* code)
2886 */
2887int fit_conf_get_node (const void *fit, const char *conf_uname)
2888{
2889 int noffset, confs_noffset;
2890 int len;
2891
2892 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2893 if (confs_noffset < 0) {
2894 debug ("Can't find configurations parent node '%s' (%s)\n",
2895 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2896 return confs_noffset;
2897 }
2898
2899 if (conf_uname == NULL) {
2900 /* get configuration unit name from the default property */
2901 debug ("No configuration specified, trying default...\n");
2902 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2903 if (conf_uname == NULL) {
2904 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2905 return len;
2906 }
2907 debug ("Found default configuration: '%s'\n", conf_uname);
2908 }
2909
2910 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2911 if (noffset < 0) {
2912 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2913 conf_uname, fdt_strerror (noffset));
2914 }
2915
2916 return noffset;
2917}
2918
2919static int __fit_conf_get_prop_node (const void *fit, int noffset,
2920 const char *prop_name)
2921{
2922 char *uname;
2923 int len;
2924
2925 /* get kernel image unit name from configuration kernel property */
2926 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2927 if (uname == NULL)
2928 return len;
2929
2930 return fit_image_get_node (fit, uname);
2931}
2932
2933/**
2934 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2935 * a given configuration
2936 * @fit: pointer to the FIT format image header
2937 * @noffset: configuration node offset
2938 *
2939 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2940 * configuration FIT_KERNEL_PROP property and translates it to the node
2941 * offset.
2942 *
2943 * returns:
2944 * image node offset when found (>=0)
2945 * negative number on failure (FDT_ERR_* code)
2946 */
2947int fit_conf_get_kernel_node (const void *fit, int noffset)
2948{
2949 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2950}
2951
2952/**
2953 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2954 * a given configuration
2955 * @fit: pointer to the FIT format image header
2956 * @noffset: configuration node offset
2957 *
2958 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2959 * configuration FIT_KERNEL_PROP property and translates it to the node
2960 * offset.
2961 *
2962 * returns:
2963 * image node offset when found (>=0)
2964 * negative number on failure (FDT_ERR_* code)
2965 */
2966int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2967{
2968 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2969}
2970
2971/**
2972 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2973 * a given configuration
2974 * @fit: pointer to the FIT format image header
2975 * @noffset: configuration node offset
2976 *
2977 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2978 * configuration FIT_KERNEL_PROP property and translates it to the node
2979 * offset.
2980 *
2981 * returns:
2982 * image node offset when found (>=0)
2983 * negative number on failure (FDT_ERR_* code)
2984 */
2985int fit_conf_get_fdt_node (const void *fit, int noffset)
2986{
2987 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2988}
2989
2990/**
2991 * fit_conf_print - prints out the FIT configuration details
2992 * @fit: pointer to the FIT format image header
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +01002993 * @noffset: offset of the configuration node
Marian Balakowicze227fbb2008-02-29 21:24:06 +01002994 * @p: pointer to prefix string
2995 *
2996 * fit_conf_print() lists all mandatory properies for the processed
2997 * configuration node.
2998 *
2999 * returns:
3000 * no returned results
3001 */
3002void fit_conf_print (const void *fit, int noffset, const char *p)
3003{
3004 char *desc;
3005 char *uname;
3006 int ret;
3007
3008 /* Mandatory properties */
3009 ret = fit_get_desc (fit, noffset, &desc);
3010 printf ("%s Description: ", p);
3011 if (ret)
3012 printf ("unavailable\n");
3013 else
3014 printf ("%s\n", desc);
3015
3016 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
3017 printf ("%s Kernel: ", p);
3018 if (uname == NULL)
3019 printf ("unavailable\n");
3020 else
3021 printf ("%s\n", uname);
3022
3023 /* Optional properties */
3024 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
3025 if (uname)
3026 printf ("%s Init Ramdisk: %s\n", p, uname);
3027
3028 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
3029 if (uname)
3030 printf ("%s FDT: %s\n", p, uname);
3031}
Marian Balakowicza50d5152008-03-12 10:12:37 +01003032
3033/**
3034 * fit_check_ramdisk - verify FIT format ramdisk subimage
3035 * @fit_hdr: pointer to the FIT ramdisk header
3036 * @rd_noffset: ramdisk subimage node offset within FIT image
3037 * @arch: requested ramdisk image architecture type
3038 * @verify: data CRC verification flag
3039 *
3040 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
3041 * specified FIT image.
3042 *
3043 * returns:
3044 * 1, on success
3045 * 0, on failure
3046 */
3047#ifndef USE_HOSTCC
3048static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
3049{
3050 fit_image_print (fit, rd_noffset, " ");
3051
3052 if (verify) {
3053 puts (" Verifying Hash Integrity ... ");
3054 if (!fit_image_check_hashes (fit, rd_noffset)) {
3055 puts ("Bad Data Hash\n");
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003056 show_boot_progress (-125);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003057 return 0;
3058 }
3059 puts ("OK\n");
3060 }
3061
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003062 show_boot_progress (126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003063 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3064 !fit_image_check_arch (fit, rd_noffset, arch) ||
3065 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3066 printf ("No Linux %s Ramdisk Image\n",
3067 genimg_get_arch_name(arch));
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003068 show_boot_progress (-126);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003069 return 0;
3070 }
3071
Marian Balakowicz74eb4ae2008-03-12 10:33:01 +01003072 show_boot_progress (127);
Marian Balakowicza50d5152008-03-12 10:12:37 +01003073 return 1;
3074}
3075#endif /* USE_HOSTCC */
Marian Balakowicz3413e222008-02-21 17:20:20 +01003076#endif /* CONFIG_FIT */