blob: 94f01ad455d3d018770b13bb8345ab337dec5446 [file] [log] [blame]
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010025
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010027#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
Marian Balakowicz95d449a2008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicz2242f532008-02-21 17:27:41 +010042#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
43#include <rtc.h>
44#endif
45
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010046#include <image.h>
47
Marian Balakowiczc8779642008-03-12 10:12:37 +010048#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +010049#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010052#endif
53
54#if defined(CONFIG_FIT)
Andy Fleming20a14a42008-04-02 16:19:07 -050055#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010056#include <sha1.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010057
58static int fit_check_ramdisk (const void *fit, int os_noffset,
59 uint8_t arch, int verify);
Marian Balakowiczfff888a12008-02-21 17:20:19 +010060#endif
61
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010062#ifdef CONFIG_CMD_BDI
63extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
64#endif
65
66DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010067
Marian Balakowiczd985c842008-03-12 10:14:38 +010068static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
69 int verify);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010070#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010071#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050072#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010073#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010074#include <image.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010075#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010076
Marian Balakowicz570abb02008-02-29 15:59:59 +010077typedef struct table_entry {
78 int id; /* as defined in image.h */
79 char *sname; /* short (input) name */
80 char *lname; /* long (output) name */
81} table_entry_t;
82
83static table_entry_t uimage_arch[] = {
84 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
85 { IH_ARCH_ALPHA, "alpha", "Alpha", },
86 { IH_ARCH_ARM, "arm", "ARM", },
87 { IH_ARCH_I386, "x86", "Intel x86", },
88 { IH_ARCH_IA64, "ia64", "IA64", },
89 { IH_ARCH_M68K, "m68k", "M68K", },
90 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
91 { IH_ARCH_MIPS, "mips", "MIPS", },
92 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
93 { IH_ARCH_NIOS, "nios", "NIOS", },
94 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070095 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010096 { IH_ARCH_PPC, "ppc", "PowerPC", },
97 { IH_ARCH_S390, "s390", "IBM S390", },
98 { IH_ARCH_SH, "sh", "SuperH", },
99 { IH_ARCH_SPARC, "sparc", "SPARC", },
100 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
101 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
102 { IH_ARCH_AVR32, "avr32", "AVR32", },
103 { -1, "", "", },
104};
105
106static table_entry_t uimage_os[] = {
107 { IH_OS_INVALID, NULL, "Invalid OS", },
108#if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
109 { IH_OS_ARTOS, "artos", "ARTOS", },
110#endif
111 { IH_OS_LINUX, "linux", "Linux", },
112#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
113 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
114#endif
115 { IH_OS_NETBSD, "netbsd", "NetBSD", },
116 { IH_OS_RTEMS, "rtems", "RTEMS", },
117 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
118#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
119 { IH_OS_QNX, "qnx", "QNX", },
120 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
121#endif
122#ifdef USE_HOSTCC
123 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
124 { IH_OS_DELL, "dell", "Dell", },
125 { IH_OS_ESIX, "esix", "Esix", },
126 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
127 { IH_OS_IRIX, "irix", "Irix", },
128 { IH_OS_NCR, "ncr", "NCR", },
129 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
130 { IH_OS_PSOS, "psos", "pSOS", },
131 { IH_OS_SCO, "sco", "SCO", },
132 { IH_OS_SOLARIS, "solaris", "Solaris", },
133 { IH_OS_SVR4, "svr4", "SVR4", },
134#endif
135 { -1, "", "", },
136};
137
138static table_entry_t uimage_type[] = {
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
140 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
141 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
142 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
143 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
146 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
147 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
148 { -1, "", "", },
149};
150
151static table_entry_t uimage_comp[] = {
152 { IH_COMP_NONE, "none", "uncompressed", },
153 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
154 { IH_COMP_GZIP, "gzip", "gzip compressed", },
155 { -1, "", "", },
156};
157
Mike Frysinger89cdab72008-03-31 11:02:01 -0400158uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200159uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100160static void genimg_print_size (uint32_t size);
161#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
162static void genimg_print_time (time_t timestamp);
163#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100164
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100165/*****************************************************************************/
166/* Legacy format routines */
167/*****************************************************************************/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100168int image_check_hcrc (image_header_t *hdr)
169{
170 ulong hcrc;
171 ulong len = image_get_header_size ();
172 image_header_t header;
173
174 /* Copy header so we can blank CRC field for re-calculation */
175 memmove (&header, (char *)hdr, image_get_header_size ());
176 image_set_hcrc (&header, 0);
177
178 hcrc = crc32 (0, (unsigned char *)&header, len);
179
180 return (hcrc == image_get_hcrc (hdr));
181}
182
183int image_check_dcrc (image_header_t *hdr)
184{
185 ulong data = image_get_data (hdr);
186 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200187 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100188
189 return (dcrc == image_get_dcrc (hdr));
190}
191
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100192/**
193 * image_multi_count - get component (sub-image) count
194 * @hdr: pointer to the header of the multi component image
195 *
196 * image_multi_count() returns number of components in a multi
197 * component image.
198 *
199 * Note: no checking of the image type is done, caller must pass
200 * a valid multi component image.
201 *
202 * returns:
203 * number of components
204 */
205ulong image_multi_count (image_header_t *hdr)
206{
207 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100208 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100209
210 /* get start of the image payload, which in case of multi
211 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100212 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100213
214 /* count non empty slots */
215 for (i = 0; size[i]; ++i)
216 count++;
217
218 return count;
219}
220
221/**
222 * image_multi_getimg - get component data address and size
223 * @hdr: pointer to the header of the multi component image
224 * @idx: index of the requested component
225 * @data: pointer to a ulong variable, will hold component data address
226 * @len: pointer to a ulong variable, will hold component size
227 *
228 * image_multi_getimg() returns size and data address for the requested
229 * component in a multi component image.
230 *
231 * Note: no checking of the image type is done, caller must pass
232 * a valid multi component image.
233 *
234 * returns:
235 * data address and size of the component, if idx is valid
236 * 0 in data and len, if idx is out of range
237 */
238void image_multi_getimg (image_header_t *hdr, ulong idx,
239 ulong *data, ulong *len)
240{
241 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100242 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700243 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100244
245 /* get number of component */
246 count = image_multi_count (hdr);
247
248 /* get start of the image payload, which in case of multi
249 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100250 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100251
252 /* get address of the proper component data start, which means
253 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100254 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100255
256 if (idx < count) {
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100257 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100258 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100259
260 /* go over all indices preceding requested component idx */
261 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700262 /* add up i-th component size, rounding up to 4 bytes */
263 offset += (uimage_to_cpu (size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100264 }
265
266 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700267 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100268 } else {
269 *len = 0;
270 *data = 0;
271 }
272}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100273
Marian Balakowicz2242f532008-02-21 17:27:41 +0100274static void image_print_type (image_header_t *hdr)
275{
276 const char *os, *arch, *type, *comp;
277
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100278 os = genimg_get_os_name (image_get_os (hdr));
279 arch = genimg_get_arch_name (image_get_arch (hdr));
280 type = genimg_get_type_name (image_get_type (hdr));
281 comp = genimg_get_comp_name (image_get_comp (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100282
Marian Balakowicz570abb02008-02-29 15:59:59 +0100283 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100284}
285
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100286/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200287 * image_print_contents - prints out the contents of the legacy format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100288 * @hdr: pointer to the legacy format image header
289 * @p: pointer to prefix string
290 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200291 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100292 * The routine prints out all header fields followed by the size/offset data
293 * for MULTI/SCRIPT images.
294 *
295 * returns:
296 * no returned results
297 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200298void image_print_contents (image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100299{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200300 const char *p;
301
302#ifdef USE_HOSTCC
303 p = "";
304#else
305 p = " ";
306#endif
307
Marian Balakowicz570abb02008-02-29 15:59:59 +0100308 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
309#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
310 printf ("%sCreated: ", p);
311 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100312#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100313 printf ("%sImage Type: ", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100314 image_print_type (hdr);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100315 printf ("%sData Size: ", p);
316 genimg_print_size (image_get_data_size (hdr));
317 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
318 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100319
Marian Balakowicz570abb02008-02-29 15:59:59 +0100320 if (image_check_type (hdr, IH_TYPE_MULTI) ||
321 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100322 int i;
323 ulong data, len;
324 ulong count = image_multi_count (hdr);
325
Marian Balakowicz570abb02008-02-29 15:59:59 +0100326 printf ("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100327 for (i = 0; i < count; i++) {
328 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100329
330 printf ("%s Image %d: ", p, i);
331 genimg_print_size (len);
332
333 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
334 /*
335 * the user may need to know offsets
336 * if planning to do something with
337 * multiple files
338 */
339 printf ("%s Offset = 0x%08lx\n", p, data);
340 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100341 }
342 }
343}
344
Marian Balakowicz570abb02008-02-29 15:59:59 +0100345
346#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100347/**
348 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100349 * @rd_addr: ramdisk image start address
350 * @arch: expected ramdisk architecture
351 * @verify: checksum verification flag
352 *
353 * image_get_ramdisk() returns a pointer to the verified ramdisk image
354 * header. Routine receives image start address and expected architecture
355 * flag. Verification done covers data and header integrity and os/type/arch
356 * fields checking.
357 *
358 * If dataflash support is enabled routine checks for dataflash addresses
359 * and handles required dataflash reads.
360 *
361 * returns:
362 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600363 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100364 */
Marian Balakowiczd985c842008-03-12 10:14:38 +0100365static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
366 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100367{
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100368 image_header_t *rd_hdr = (image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100369
370 if (!image_check_magic (rd_hdr)) {
371 puts ("Bad Magic Number\n");
372 show_boot_progress (-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600373 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100374 }
375
376 if (!image_check_hcrc (rd_hdr)) {
377 puts ("Bad Header Checksum\n");
378 show_boot_progress (-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600379 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100380 }
381
382 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100383 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100384
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100385 if (verify) {
386 puts(" Verifying Checksum ... ");
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200387 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100388 puts ("Bad Data CRC\n");
389 show_boot_progress (-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600390 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100391 }
392 puts("OK\n");
393 }
394
395 show_boot_progress (11);
396
397 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
398 !image_check_arch (rd_hdr, arch) ||
399 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
400 printf ("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100401 genimg_get_arch_name(arch));
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100402 show_boot_progress (-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600403 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100404 }
405
406 return rd_hdr;
407}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100408#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100409
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100410/*****************************************************************************/
411/* Shared dual-format routines */
412/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100413#ifndef USE_HOSTCC
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200414int getenv_yesno (char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100415{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200416 char *s = getenv (var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100417 return (s && (*s == 'n')) ? 0 : 1;
418}
419
420ulong getenv_bootm_low(void)
421{
422 char *s = getenv ("bootm_low");
423 if (s) {
424 ulong tmp = simple_strtoul (s, NULL, 16);
425 return tmp;
426 }
427
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100428#if defined(CFG_SDRAM_BASE)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100429 return CFG_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100430#elif defined(CONFIG_ARM)
431 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100432#else
433 return 0;
434#endif
435}
436
Becky Bruce391fd932008-06-09 20:37:18 -0500437phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100438{
439 char *s = getenv ("bootm_size");
440 if (s) {
Becky Bruce391fd932008-06-09 20:37:18 -0500441 phys_size_t tmp;
442#ifdef CFG_64BIT_STRTOUL
443 tmp = (phys_size_t)simple_strtoull (s, NULL, 16);
444#else
445 tmp = (phys_size_t)simple_strtoul (s, NULL, 16);
446#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100447 return tmp;
448 }
449
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100450#if defined(CONFIG_ARM)
451 return gd->bd->bi_dram[0].size;
452#else
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100453 return gd->bd->bi_memsize;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100454#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100455}
456
457void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
458{
459#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
460 while (len > 0) {
461 size_t tail = (len > chunksz) ? chunksz : len;
462 WATCHDOG_RESET ();
463 memmove (to, from, tail);
464 to += tail;
465 from += tail;
466 len -= tail;
467 }
468#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
469 memmove (to, from, len);
470#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
471}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100472#endif /* !USE_HOSTCC */
473
474static void genimg_print_size (uint32_t size)
475{
476#ifndef USE_HOSTCC
477 printf ("%d Bytes = ", size);
478 print_size (size, "\n");
479#else
480 printf ("%d Bytes = %.2f kB = %.2f MB\n",
481 size, (double)size / 1.024e3,
482 (double)size / 1.048576e6);
483#endif
484}
485
486#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
487static void genimg_print_time (time_t timestamp)
488{
489#ifndef USE_HOSTCC
490 struct rtc_time tm;
491
492 to_tm (timestamp, &tm);
493 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
494 tm.tm_year, tm.tm_mon, tm.tm_mday,
495 tm.tm_hour, tm.tm_min, tm.tm_sec);
496#else
497 printf ("%s", ctime(&timestamp));
498#endif
499}
500#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
501
502/**
503 * get_table_entry_name - translate entry id to long name
504 * @table: pointer to a translation table for entries of a specific type
505 * @msg: message to be returned when translation fails
506 * @id: entry id to be translated
507 *
508 * get_table_entry_name() will go over translation table trying to find
509 * entry that matches given id. If matching entry is found, its long
510 * name is returned to the caller.
511 *
512 * returns:
513 * long entry name if translation succeeds
514 * msg otherwise
515 */
516static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
517{
518 for (; table->id >= 0; ++table) {
519 if (table->id == id)
520 return (table->lname);
521 }
522 return (msg);
523}
524
525const char *genimg_get_os_name (uint8_t os)
526{
527 return (get_table_entry_name (uimage_os, "Unknown OS", os));
528}
529
530const char *genimg_get_arch_name (uint8_t arch)
531{
532 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
533}
534
535const char *genimg_get_type_name (uint8_t type)
536{
537 return (get_table_entry_name (uimage_type, "Unknown Image", type));
538}
539
540const char *genimg_get_comp_name (uint8_t comp)
541{
542 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
543}
544
545/**
546 * get_table_entry_id - translate short entry name to id
547 * @table: pointer to a translation table for entries of a specific type
548 * @table_name: to be used in case of error
549 * @name: entry short name to be translated
550 *
551 * get_table_entry_id() will go over translation table trying to find
552 * entry that matches given short name. If matching entry is found,
553 * its id returned to the caller.
554 *
555 * returns:
556 * entry id if translation succeeds
557 * -1 otherwise
558 */
559static int get_table_entry_id (table_entry_t *table,
560 const char *table_name, const char *name)
561{
562 table_entry_t *t;
563#ifdef USE_HOSTCC
564 int first = 1;
565
566 for (t = table; t->id >= 0; ++t) {
567 if (t->sname && strcasecmp(t->sname, name) == 0)
568 return (t->id);
569 }
570
571 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
572 for (t = table; t->id >= 0; ++t) {
573 if (t->sname == NULL)
574 continue;
575 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
576 first = 0;
577 }
578 fprintf (stderr, "\n");
579#else
580 for (t = table; t->id >= 0; ++t) {
581 if (t->sname && strcmp(t->sname, name) == 0)
582 return (t->id);
583 }
584 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100585#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100586 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100587}
588
Marian Balakowicz570abb02008-02-29 15:59:59 +0100589int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100590{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100591 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100592}
593
Marian Balakowicz570abb02008-02-29 15:59:59 +0100594int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100595{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100596 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100597}
598
Marian Balakowicz570abb02008-02-29 15:59:59 +0100599int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100600{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100601 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100602}
603
Marian Balakowicz570abb02008-02-29 15:59:59 +0100604int genimg_get_comp_id (const char *name)
605{
606 return (get_table_entry_id (uimage_comp, "Compression", name));
607}
608
609#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100610/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100611 * genimg_get_format - get image format type
612 * @img_addr: image start address
613 *
614 * genimg_get_format() checks whether provided address points to a valid
615 * legacy or FIT image.
616 *
617 * New uImage format and FDT blob are based on a libfdt. FDT blob
618 * may be passed directly or embedded in a FIT image. In both situations
619 * genimg_get_format() must be able to dectect libfdt header.
620 *
621 * returns:
622 * image format type or IMAGE_FORMAT_INVALID if no image is present
623 */
624int genimg_get_format (void *img_addr)
625{
626 ulong format = IMAGE_FORMAT_INVALID;
627 image_header_t *hdr;
628#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
629 char *fit_hdr;
630#endif
631
632 hdr = (image_header_t *)img_addr;
633 if (image_check_magic(hdr))
634 format = IMAGE_FORMAT_LEGACY;
635#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
636 else {
637 fit_hdr = (char *)img_addr;
638 if (fdt_check_header (fit_hdr) == 0)
639 format = IMAGE_FORMAT_FIT;
640 }
641#endif
642
643 return format;
644}
645
646/**
647 * genimg_get_image - get image from special storage (if necessary)
648 * @img_addr: image start address
649 *
650 * genimg_get_image() checks if provided image start adddress is located
651 * in a dataflash storage. If so, image is moved to a system RAM memory.
652 *
653 * returns:
654 * image start address after possible relocation from special storage
655 */
656ulong genimg_get_image (ulong img_addr)
657{
658 ulong ram_addr = img_addr;
659
660#ifdef CONFIG_HAS_DATAFLASH
661 ulong h_size, d_size;
662
663 if (addr_dataflash (img_addr)){
664 /* ger RAM address */
665 ram_addr = CFG_LOAD_ADDR;
666
667 /* get header size */
668 h_size = image_get_header_size ();
669#if defined(CONFIG_FIT)
670 if (sizeof(struct fdt_header) > h_size)
671 h_size = sizeof(struct fdt_header);
672#endif
673
674 /* read in header */
675 debug (" Reading image header from dataflash address "
676 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
677
678 read_dataflash (img_addr, h_size, (char *)ram_addr);
679
680 /* get data size */
681 switch (genimg_get_format ((void *)ram_addr)) {
682 case IMAGE_FORMAT_LEGACY:
683 d_size = image_get_data_size ((image_header_t *)ram_addr);
684 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
685 ram_addr, d_size);
686 break;
687#if defined(CONFIG_FIT)
688 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100689 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100690 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
691 ram_addr, d_size);
692 break;
693#endif
694 default:
695 printf (" No valid image found at 0x%08lx\n", img_addr);
696 return ram_addr;
697 }
698
699 /* read in image data */
700 debug (" Reading image remaining data from dataflash address "
701 "%08lx to RAM address %08lx\n", img_addr + h_size,
702 ram_addr + h_size);
703
704 read_dataflash (img_addr + h_size, d_size,
705 (char *)(ram_addr + h_size));
706
707 }
708#endif /* CONFIG_HAS_DATAFLASH */
709
710 return ram_addr;
711}
712
713/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100714 * fit_has_config - check if there is a valid FIT configuration
715 * @images: pointer to the bootm command headers structure
716 *
717 * fit_has_config() checks if there is a FIT configuration in use
718 * (if FTI support is present).
719 *
720 * returns:
721 * 0, no FIT support or no configuration found
722 * 1, configuration found
723 */
724int genimg_has_config (bootm_headers_t *images)
725{
726#if defined(CONFIG_FIT)
727 if (images->fit_uname_cfg)
728 return 1;
729#endif
730 return 0;
731}
732
733/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100734 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100735 * @argc: command argument count
736 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100737 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100738 * @arch: expected ramdisk architecture
739 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
740 * @rd_end: pointer to a ulong variable, will hold ramdisk end
741 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100742 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100743 * Curently supported are the following ramdisk sources:
744 * - multicomponent kernel/ramdisk image,
745 * - commandline provided address of decicated ramdisk image.
746 *
747 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100748 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100749 * rd_start and rd_end are set to ramdisk start/end addresses if
750 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100751 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500752 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100753 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100754 */
Marian Balakowiczd985c842008-03-12 10:14:38 +0100755int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
756 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100757{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100758 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100759 ulong rd_data, rd_len;
760 image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100761#if defined(CONFIG_FIT)
762 void *fit_hdr;
763 const char *fit_uname_config = NULL;
764 const char *fit_uname_ramdisk = NULL;
765 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100766 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100767 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100768 const void *data;
769 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100770#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100771
Marian Balakowiczc8779642008-03-12 10:12:37 +0100772 *rd_start = 0;
773 *rd_end = 0;
774
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100775 /*
776 * Look for a '-' which indicates to ignore the
777 * ramdisk argument
778 */
779 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
780 debug ("## Skipping init Ramdisk\n");
781 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100782 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100783#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100784 if (argc >= 3) {
785 /*
786 * If the init ramdisk comes from the FIT image and
787 * the FIT image address is omitted in the command
788 * line argument, try to use os FIT image address or
789 * default load address.
790 */
791 if (images->fit_uname_os)
792 default_addr = (ulong)images->fit_hdr_os;
793 else
794 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100795
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100796 if (fit_parse_conf (argv[2], default_addr,
797 &rd_addr, &fit_uname_config)) {
798 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
799 fit_uname_config, rd_addr);
800 } else if (fit_parse_subimage (argv[2], default_addr,
801 &rd_addr, &fit_uname_ramdisk)) {
802 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
803 fit_uname_ramdisk, rd_addr);
804 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100805#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100806 {
807 rd_addr = simple_strtoul(argv[2], NULL, 16);
808 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
809 rd_addr);
810 }
811#if defined(CONFIG_FIT)
812 } else {
813 /* use FIT configuration provided in first bootm
814 * command argument
815 */
816 rd_addr = (ulong)images->fit_hdr_os;
817 fit_uname_config = images->fit_uname_cfg;
818 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
819 fit_uname_config, rd_addr);
820
821 /*
822 * Check whether configuration has ramdisk defined,
823 * if not, don't try to use it, quit silently.
824 */
825 fit_hdr = (void *)rd_addr;
826 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
827 if (cfg_noffset < 0) {
828 debug ("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200829 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100830 }
831
832 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
833 if (rd_noffset < 0) {
834 debug ("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500835 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100836 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100837 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100838#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100839
840 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100841 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100842
843 /*
844 * Check if there is an initrd image at the
845 * address provided in the second bootm argument
846 * check image type, for FIT images get FIT node.
847 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100848 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100849 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100850 printf ("## Loading init Ramdisk from Legacy "
851 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100852
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100853 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100854 rd_hdr = image_get_ramdisk (rd_addr, arch,
855 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100856
Marian Balakowiczc8779642008-03-12 10:12:37 +0100857 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600858 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600859
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100860 rd_data = image_get_data (rd_hdr);
861 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100862 rd_load = image_get_load (rd_hdr);
863 break;
864#if defined(CONFIG_FIT)
865 case IMAGE_FORMAT_FIT:
866 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100867 printf ("## Loading init Ramdisk from FIT "
868 "Image at %08lx ...\n", rd_addr);
869
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100870 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100871 if (!fit_check_format (fit_hdr)) {
872 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100873 show_boot_progress (-120);
Michal Simekc78fce62008-07-11 10:43:13 +0200874 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100875 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100876 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100877
878 if (!fit_uname_ramdisk) {
879 /*
880 * no ramdisk image node unit name, try to get config
881 * node first. If config unit node name is NULL
882 * fit_conf_get_node() will try to find default config node
883 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100884 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100885 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
886 if (cfg_noffset < 0) {
887 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100888 show_boot_progress (-122);
Michal Simekc78fce62008-07-11 10:43:13 +0200889 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100890 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100891 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
892 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100893
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100894 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100895 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
896 } else {
897 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100898 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100899 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
900 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100901 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100902 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100903 show_boot_progress (-124);
Michal Simekc78fce62008-07-11 10:43:13 +0200904 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100905 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100906
907 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
908
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100909 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100910 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200911 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100912
913 /* get ramdisk image data address and length */
914 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
915 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100916 show_boot_progress (-127);
Michal Simekc78fce62008-07-11 10:43:13 +0200917 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100918 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100919 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100920
921 rd_data = (ulong)data;
922 rd_len = size;
923
924 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
925 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100926 show_boot_progress (-129);
Michal Simekc78fce62008-07-11 10:43:13 +0200927 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100928 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100929 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100930
931 images->fit_hdr_rd = fit_hdr;
932 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100933 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100934 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100935#endif
936 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100937 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100938 rd_data = rd_len = rd_load = 0;
Kumar Galaea86b9e2008-08-29 19:08:29 -0500939 return 1;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100940 }
941
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100942#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100943 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100944 * We need to copy the ramdisk to SRAM to let Linux boot
945 */
946 if (rd_data) {
947 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
948 rd_data = rd_load;
949 }
950#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
951
952 } else if (images->legacy_hdr_valid &&
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200953 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100954 /*
955 * Now check if we have a legacy mult-component image,
956 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100957 */
958 show_boot_progress (13);
959 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100960 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100961 (ulong)images->legacy_hdr_os);
962
963 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100964 } else {
965 /*
966 * no initrd image
967 */
968 show_boot_progress (14);
969 rd_len = rd_data = 0;
970 }
971
972 if (!rd_data) {
973 debug ("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100974 } else {
975 *rd_start = rd_data;
976 *rd_end = rd_data + rd_len;
977 }
978 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
979 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -0600980
981 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100982}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100983
Daniel Hellstrombf3d8b32008-03-28 08:29:26 +0100984#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100985/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100986 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -0600987 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100988 * @rd_data: ramdisk data start address
989 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100990 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
991 * start address (after possible relocation)
992 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
993 * end address (after possible relocation)
994 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100995 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100996 * variable and if requested ramdisk data is moved to a specified location.
997 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100998 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
999 * start/end addresses if ramdisk image start and len were provided,
1000 * otherwise set initrd_start and initrd_end set to zeros.
1001 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001002 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001003 * 0 - success
1004 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001005 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001006int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001007 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001008{
1009 char *s;
1010 ulong initrd_high;
1011 int initrd_copy_to_ram = 1;
1012
1013 if ((s = getenv ("initrd_high")) != NULL) {
1014 /* a value of "no" or a similar string will act like 0,
1015 * turning the "load high" feature off. This is intentional.
1016 */
1017 initrd_high = simple_strtoul (s, NULL, 16);
1018 if (initrd_high == ~0)
1019 initrd_copy_to_ram = 0;
1020 } else {
1021 /* not set, no restrictions to load high */
1022 initrd_high = ~0;
1023 }
1024
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001025
1026#ifdef CONFIG_LOGBUFFER
1027 /* Prevent initrd from overwriting logbuffer */
1028 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1029#endif
1030
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001031 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1032 initrd_high, initrd_copy_to_ram);
1033
1034 if (rd_data) {
1035 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1036 debug (" in-place initrd\n");
1037 *initrd_start = rd_data;
1038 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001039 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001040 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001041 if (initrd_high)
Becky Bruce391fd932008-06-09 20:37:18 -05001042 *initrd_start = (ulong)lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001043 else
Becky Bruce391fd932008-06-09 20:37:18 -05001044 *initrd_start = (ulong)lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001045
Kumar Galae822d7f2008-02-27 21:51:49 -06001046 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001047 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001048 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001049 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001050 show_boot_progress (12);
1051
1052 *initrd_end = *initrd_start + rd_len;
1053 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1054 *initrd_start, *initrd_end);
1055
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001056 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001057 (void *)rd_data, rd_len, CHUNKSZ);
1058
1059 puts ("OK\n");
1060 }
1061 } else {
1062 *initrd_start = 0;
1063 *initrd_end = 0;
1064 }
1065 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1066 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001067
Kumar Galae822d7f2008-02-27 21:51:49 -06001068 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001069
Kumar Galae822d7f2008-02-27 21:51:49 -06001070error:
1071 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001072}
1073
Kumar Gala06a09912008-08-15 08:24:38 -05001074#ifdef CONFIG_OF_LIBFDT
1075static void fdt_error (const char *msg)
1076{
1077 puts ("ERROR: ");
1078 puts (msg);
1079 puts (" - must RESET the board to recover.\n");
1080}
1081
1082static image_header_t *image_get_fdt (ulong fdt_addr)
1083{
1084 image_header_t *fdt_hdr = (image_header_t *)fdt_addr;
1085
1086 image_print_contents (fdt_hdr);
1087
1088 puts (" Verifying Checksum ... ");
1089 if (!image_check_hcrc (fdt_hdr)) {
1090 fdt_error ("fdt header checksum invalid");
1091 return NULL;
1092 }
1093
1094 if (!image_check_dcrc (fdt_hdr)) {
1095 fdt_error ("fdt checksum invalid");
1096 return NULL;
1097 }
1098 puts ("OK\n");
1099
1100 if (!image_check_type (fdt_hdr, IH_TYPE_FLATDT)) {
1101 fdt_error ("uImage is not a fdt");
1102 return NULL;
1103 }
1104 if (image_get_comp (fdt_hdr) != IH_COMP_NONE) {
1105 fdt_error ("uImage is compressed");
1106 return NULL;
1107 }
1108 if (fdt_check_header ((char *)image_get_data (fdt_hdr)) != 0) {
1109 fdt_error ("uImage data is not a fdt");
1110 return NULL;
1111 }
1112 return fdt_hdr;
1113}
1114
1115/**
1116 * fit_check_fdt - verify FIT format FDT subimage
1117 * @fit_hdr: pointer to the FIT header
1118 * fdt_noffset: FDT subimage node offset within FIT image
1119 * @verify: data CRC verification flag
1120 *
1121 * fit_check_fdt() verifies integrity of the FDT subimage and from
1122 * specified FIT image.
1123 *
1124 * returns:
1125 * 1, on success
1126 * 0, on failure
1127 */
1128#if defined(CONFIG_FIT)
1129static int fit_check_fdt (const void *fit, int fdt_noffset, int verify)
1130{
1131 fit_image_print (fit, fdt_noffset, " ");
1132
1133 if (verify) {
1134 puts (" Verifying Hash Integrity ... ");
1135 if (!fit_image_check_hashes (fit, fdt_noffset)) {
1136 fdt_error ("Bad Data Hash");
1137 return 0;
1138 }
1139 puts ("OK\n");
1140 }
1141
1142 if (!fit_image_check_type (fit, fdt_noffset, IH_TYPE_FLATDT)) {
1143 fdt_error ("Not a FDT image");
1144 return 0;
1145 }
1146
1147 if (!fit_image_check_comp (fit, fdt_noffset, IH_COMP_NONE)) {
1148 fdt_error ("FDT image is compressed");
1149 return 0;
1150 }
1151
1152 return 1;
1153}
1154#endif /* CONFIG_FIT */
1155
1156#ifndef CFG_FDT_PAD
1157#define CFG_FDT_PAD 0x3000
1158#endif
1159
1160/**
1161 * boot_relocate_fdt - relocate flat device tree
1162 * @lmb: pointer to lmb handle, will be used for memory mgmt
1163 * @bootmap_base: base address of the bootmap region
1164 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1165 * @of_size: pointer to a ulong variable, will hold fdt length
1166 *
1167 * boot_relocate_fdt() determines if the of_flat_tree address is within
1168 * the bootmap and if not relocates it into that region
1169 *
1170 * of_flat_tree and of_size are set to final (after relocation) values
1171 *
1172 * returns:
1173 * 0 - success
1174 * 1 - failure
1175 */
1176int boot_relocate_fdt (struct lmb *lmb, ulong bootmap_base,
1177 char **of_flat_tree, ulong *of_size)
1178{
1179 char *fdt_blob = *of_flat_tree;
1180 ulong relocate = 0;
1181 ulong of_len = 0;
1182
1183 /* nothing to do */
1184 if (*of_size == 0)
1185 return 0;
1186
1187 if (fdt_check_header (fdt_blob) != 0) {
1188 fdt_error ("image is not a fdt");
1189 goto error;
1190 }
1191
1192#ifndef CFG_NO_FLASH
1193 /* move the blob if it is in flash (set relocate) */
1194 if (addr2info ((ulong)fdt_blob) != NULL)
1195 relocate = 1;
1196#endif
1197
1198 /*
1199 * The blob needs to be inside the boot mapping.
1200 */
1201 if (fdt_blob < (char *)bootmap_base)
1202 relocate = 1;
1203
1204 if ((fdt_blob + *of_size + CFG_FDT_PAD) >=
1205 ((char *)CFG_BOOTMAPSZ + bootmap_base))
1206 relocate = 1;
1207
1208 /* move flattend device tree if needed */
1209 if (relocate) {
1210 int err;
1211 ulong of_start = 0;
1212
1213 /* position on a 4K boundary before the alloc_current */
1214 /* Pad the FDT by a specified amount */
1215 of_len = *of_size + CFG_FDT_PAD;
1216 of_start = (unsigned long)lmb_alloc_base(lmb, of_len, 0x1000,
1217 (CFG_BOOTMAPSZ + bootmap_base));
1218
1219 if (of_start == 0) {
1220 puts("device tree - allocation error\n");
1221 goto error;
1222 }
1223
1224 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
1225 (ulong)fdt_blob, (ulong)fdt_blob + *of_size - 1,
1226 of_len, of_len);
1227
1228 printf (" Loading Device Tree to %08lx, end %08lx ... ",
1229 of_start, of_start + of_len - 1);
1230
1231 err = fdt_open_into (fdt_blob, (void *)of_start, of_len);
1232 if (err != 0) {
1233 fdt_error ("fdt move failed");
1234 goto error;
1235 }
1236 puts ("OK\n");
1237
1238 *of_flat_tree = (char *)of_start;
1239 *of_size = of_len;
1240 } else {
1241 *of_flat_tree = fdt_blob;
1242 of_len = (CFG_BOOTMAPSZ + bootmap_base) - (ulong)fdt_blob;
1243 lmb_reserve(lmb, (ulong)fdt_blob, of_len);
1244 fdt_set_totalsize(*of_flat_tree, of_len);
1245
1246 *of_size = of_len;
1247 }
1248
Kumar Gala54f9c862008-08-15 08:24:39 -05001249 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001250 return 0;
1251
1252error:
1253 return 1;
1254}
1255
1256/**
1257 * boot_get_fdt - main fdt handling routine
1258 * @argc: command argument count
1259 * @argv: command argument list
1260 * @images: pointer to the bootm images structure
1261 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1262 * @of_size: pointer to a ulong variable, will hold fdt length
1263 *
1264 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1265 * Curently supported are the following ramdisk sources:
1266 * - multicomponent kernel/ramdisk image,
1267 * - commandline provided address of decicated ramdisk image.
1268 *
1269 * returns:
1270 * 0, if fdt image was found and valid, or skipped
1271 * of_flat_tree and of_size are set to fdt start address and length if
1272 * fdt image is found and valid
1273 *
1274 * 1, if fdt image is found but corrupted
1275 * of_flat_tree and of_size are set to 0 if no fdt exists
1276 */
1277int boot_get_fdt (int flag, int argc, char *argv[], bootm_headers_t *images,
1278 char **of_flat_tree, ulong *of_size)
1279{
1280 ulong fdt_addr;
1281 image_header_t *fdt_hdr;
1282 char *fdt_blob = NULL;
1283 ulong image_start, image_end;
1284 ulong load_start, load_end;
1285#if defined(CONFIG_FIT)
1286 void *fit_hdr;
1287 const char *fit_uname_config = NULL;
1288 const char *fit_uname_fdt = NULL;
1289 ulong default_addr;
1290 int cfg_noffset;
1291 int fdt_noffset;
1292 const void *data;
1293 size_t size;
1294#endif
1295
1296 *of_flat_tree = NULL;
1297 *of_size = 0;
1298
1299 if (argc > 3 || genimg_has_config (images)) {
1300#if defined(CONFIG_FIT)
1301 if (argc > 3) {
1302 /*
1303 * If the FDT blob comes from the FIT image and the
1304 * FIT image address is omitted in the command line
1305 * argument, try to use ramdisk or os FIT image
1306 * address or default load address.
1307 */
1308 if (images->fit_uname_rd)
1309 default_addr = (ulong)images->fit_hdr_rd;
1310 else if (images->fit_uname_os)
1311 default_addr = (ulong)images->fit_hdr_os;
1312 else
1313 default_addr = load_addr;
1314
1315 if (fit_parse_conf (argv[3], default_addr,
1316 &fdt_addr, &fit_uname_config)) {
1317 debug ("* fdt: config '%s' from image at 0x%08lx\n",
1318 fit_uname_config, fdt_addr);
1319 } else if (fit_parse_subimage (argv[3], default_addr,
1320 &fdt_addr, &fit_uname_fdt)) {
1321 debug ("* fdt: subimage '%s' from image at 0x%08lx\n",
1322 fit_uname_fdt, fdt_addr);
1323 } else
1324#endif
1325 {
1326 fdt_addr = simple_strtoul(argv[3], NULL, 16);
1327 debug ("* fdt: cmdline image address = 0x%08lx\n",
1328 fdt_addr);
1329 }
1330#if defined(CONFIG_FIT)
1331 } else {
1332 /* use FIT configuration provided in first bootm
1333 * command argument
1334 */
1335 fdt_addr = (ulong)images->fit_hdr_os;
1336 fit_uname_config = images->fit_uname_cfg;
1337 debug ("* fdt: using config '%s' from image at 0x%08lx\n",
1338 fit_uname_config, fdt_addr);
1339
1340 /*
1341 * Check whether configuration has FDT blob defined,
1342 * if not quit silently.
1343 */
1344 fit_hdr = (void *)fdt_addr;
1345 cfg_noffset = fit_conf_get_node (fit_hdr,
1346 fit_uname_config);
1347 if (cfg_noffset < 0) {
1348 debug ("* fdt: no such config\n");
1349 return 0;
1350 }
1351
1352 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1353 cfg_noffset);
1354 if (fdt_noffset < 0) {
1355 debug ("* fdt: no fdt in config\n");
1356 return 0;
1357 }
1358 }
1359#endif
1360
1361 debug ("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1362 fdt_addr);
1363
1364 /* copy from dataflash if needed */
1365 fdt_addr = genimg_get_image (fdt_addr);
1366
1367 /*
1368 * Check if there is an FDT image at the
1369 * address provided in the second bootm argument
1370 * check image type, for FIT images get a FIT node.
1371 */
1372 switch (genimg_get_format ((void *)fdt_addr)) {
1373 case IMAGE_FORMAT_LEGACY:
1374 /* verify fdt_addr points to a valid image header */
1375 printf ("## Flattened Device Tree from Legacy Image at %08lx\n",
1376 fdt_addr);
1377 fdt_hdr = image_get_fdt (fdt_addr);
1378 if (!fdt_hdr)
1379 goto error;
1380
1381 /*
1382 * move image data to the load address,
1383 * make sure we don't overwrite initial image
1384 */
1385 image_start = (ulong)fdt_hdr;
1386 image_end = image_get_image_end (fdt_hdr);
1387
1388 load_start = image_get_load (fdt_hdr);
1389 load_end = load_start + image_get_data_size (fdt_hdr);
1390
1391 if ((load_start < image_end) && (load_end > image_start)) {
1392 fdt_error ("fdt overwritten");
1393 goto error;
1394 }
1395
1396 debug (" Loading FDT from 0x%08lx to 0x%08lx\n",
1397 image_get_data (fdt_hdr), load_start);
1398
1399 memmove ((void *)load_start,
1400 (void *)image_get_data (fdt_hdr),
1401 image_get_data_size (fdt_hdr));
1402
1403 fdt_blob = (char *)load_start;
1404 break;
1405 case IMAGE_FORMAT_FIT:
1406 /*
1407 * This case will catch both: new uImage format
1408 * (libfdt based) and raw FDT blob (also libfdt
1409 * based).
1410 */
1411#if defined(CONFIG_FIT)
1412 /* check FDT blob vs FIT blob */
1413 if (fit_check_format ((const void *)fdt_addr)) {
1414 /*
1415 * FIT image
1416 */
1417 fit_hdr = (void *)fdt_addr;
1418 printf ("## Flattened Device Tree from FIT Image at %08lx\n",
1419 fdt_addr);
1420
1421 if (!fit_uname_fdt) {
1422 /*
1423 * no FDT blob image node unit name,
1424 * try to get config node first. If
1425 * config unit node name is NULL
1426 * fit_conf_get_node() will try to
1427 * find default config node
1428 */
1429 cfg_noffset = fit_conf_get_node (fit_hdr,
1430 fit_uname_config);
1431
1432 if (cfg_noffset < 0) {
1433 fdt_error ("Could not find configuration node\n");
1434 goto error;
1435 }
1436
1437 fit_uname_config = fdt_get_name (fit_hdr,
1438 cfg_noffset, NULL);
1439 printf (" Using '%s' configuration\n",
1440 fit_uname_config);
1441
1442 fdt_noffset = fit_conf_get_fdt_node (fit_hdr,
1443 cfg_noffset);
1444 fit_uname_fdt = fit_get_name (fit_hdr,
1445 fdt_noffset, NULL);
1446 } else {
1447 /* get FDT component image node offset */
1448 fdt_noffset = fit_image_get_node (fit_hdr,
1449 fit_uname_fdt);
1450 }
1451 if (fdt_noffset < 0) {
1452 fdt_error ("Could not find subimage node\n");
1453 goto error;
1454 }
1455
1456 printf (" Trying '%s' FDT blob subimage\n",
1457 fit_uname_fdt);
1458
1459 if (!fit_check_fdt (fit_hdr, fdt_noffset,
1460 images->verify))
1461 goto error;
1462
1463 /* get ramdisk image data address and length */
1464 if (fit_image_get_data (fit_hdr, fdt_noffset,
1465 &data, &size)) {
1466 fdt_error ("Could not find FDT subimage data");
1467 goto error;
1468 }
1469
1470 /* verift that image data is a proper FDT blob */
1471 if (fdt_check_header ((char *)data) != 0) {
1472 fdt_error ("Subimage data is not a FTD");
1473 goto error;
1474 }
1475
1476 /*
1477 * move image data to the load address,
1478 * make sure we don't overwrite initial image
1479 */
1480 image_start = (ulong)fit_hdr;
1481 image_end = fit_get_end (fit_hdr);
1482
1483 if (fit_image_get_load (fit_hdr, fdt_noffset,
1484 &load_start) == 0) {
1485 load_end = load_start + size;
1486
1487 if ((load_start < image_end) &&
1488 (load_end > image_start)) {
1489 fdt_error ("FDT overwritten");
1490 goto error;
1491 }
1492
1493 printf (" Loading FDT from 0x%08lx to 0x%08lx\n",
1494 (ulong)data, load_start);
1495
1496 memmove ((void *)load_start,
1497 (void *)data, size);
1498
1499 fdt_blob = (char *)load_start;
1500 } else {
1501 fdt_blob = (char *)data;
1502 }
1503
1504 images->fit_hdr_fdt = fit_hdr;
1505 images->fit_uname_fdt = fit_uname_fdt;
1506 images->fit_noffset_fdt = fdt_noffset;
1507 break;
1508 } else
1509#endif
1510 {
1511 /*
1512 * FDT blob
1513 */
1514 fdt_blob = (char *)fdt_addr;
1515 debug ("* fdt: raw FDT blob\n");
1516 printf ("## Flattened Device Tree blob at %08lx\n", (long)fdt_blob);
1517 }
1518 break;
1519 default:
Anatolij Gustschin8e024942008-08-29 21:04:45 +02001520 puts ("ERROR: Did not find a cmdline Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001521 goto error;
1522 }
1523
1524 printf (" Booting using the fdt blob at 0x%x\n", (int)fdt_blob);
1525
1526 } else if (images->legacy_hdr_valid &&
1527 image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1528
1529 ulong fdt_data, fdt_len;
1530
1531 /*
1532 * Now check if we have a legacy multi-component image,
1533 * get second entry data start address and len.
1534 */
1535 printf ("## Flattened Device Tree from multi "
1536 "component Image at %08lX\n",
1537 (ulong)images->legacy_hdr_os);
1538
1539 image_multi_getimg (images->legacy_hdr_os, 2, &fdt_data, &fdt_len);
1540 if (fdt_len) {
1541
1542 fdt_blob = (char *)fdt_data;
1543 printf (" Booting using the fdt at 0x%x\n", (int)fdt_blob);
1544
1545 if (fdt_check_header (fdt_blob) != 0) {
1546 fdt_error ("image is not a fdt");
1547 goto error;
1548 }
1549
1550 if (be32_to_cpu (fdt_totalsize (fdt_blob)) != fdt_len) {
1551 fdt_error ("fdt size != image size");
1552 goto error;
1553 }
1554 } else {
1555 debug ("## No Flattened Device Tree\n");
1556 return 0;
1557 }
1558 } else {
1559 debug ("## No Flattened Device Tree\n");
1560 return 0;
1561 }
1562
1563 *of_flat_tree = fdt_blob;
1564 *of_size = be32_to_cpu (fdt_totalsize (fdt_blob));
1565 debug (" of_flat_tree at 0x%08lx size 0x%08lx\n",
1566 *of_flat_tree, *of_size);
1567
1568 return 0;
1569
1570error:
1571 *of_flat_tree = 0;
1572 *of_size = 0;
1573 return 1;
1574}
1575#endif /* CONFIG_OF_LIBFDT */
1576
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001577/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001578 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001579 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001580 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1581 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galae822d7f2008-02-27 21:51:49 -06001582 * @bootmap_base: ulong variable, holds offset in physical memory to
1583 * base of bootmap
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001584 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001585 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galae822d7f2008-02-27 21:51:49 -06001586 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001587 * variable is present its contents is copied to allocated kernel
1588 * command line.
1589 *
1590 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001591 * 0 - success
1592 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001593 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001594int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galae822d7f2008-02-27 21:51:49 -06001595 ulong bootmap_base)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001596{
1597 char *cmdline;
1598 char *s;
1599
Becky Bruce391fd932008-06-09 20:37:18 -05001600 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
Kumar Galae822d7f2008-02-27 21:51:49 -06001601 CFG_BOOTMAPSZ + bootmap_base);
1602
1603 if (cmdline == NULL)
1604 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001605
1606 if ((s = getenv("bootargs")) == NULL)
1607 s = "";
1608
1609 strcpy(cmdline, s);
1610
1611 *cmd_start = (ulong) & cmdline[0];
1612 *cmd_end = *cmd_start + strlen(cmdline);
1613
1614 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1615
Kumar Galae822d7f2008-02-27 21:51:49 -06001616 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001617}
1618
1619/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001620 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001621 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001622 * @kbd: double pointer to board info data
Kumar Galae822d7f2008-02-27 21:51:49 -06001623 * @bootmap_base: ulong variable, holds offset in physical memory to
1624 * base of bootmap
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001625 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001626 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galae822d7f2008-02-27 21:51:49 -06001627 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1628 * the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001629 *
1630 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001631 * 0 - success
1632 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001633 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001634int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001635{
Becky Bruce391fd932008-06-09 20:37:18 -05001636 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Kumar Galae822d7f2008-02-27 21:51:49 -06001637 CFG_BOOTMAPSZ + bootmap_base);
1638 if (*kbd == NULL)
1639 return -1;
1640
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001641 **kbd = *(gd->bd);
1642
1643 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1644
1645#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1646 do_bdinfo(NULL, 0, 0, NULL);
1647#endif
1648
Kumar Galae822d7f2008-02-27 21:51:49 -06001649 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001650}
1651#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001652#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001653
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001654#if defined(CONFIG_FIT)
1655/*****************************************************************************/
1656/* New uImage format routines */
1657/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001658#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001659static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1660 ulong *addr, const char **name)
1661{
1662 const char *sep;
1663
1664 *addr = addr_curr;
1665 *name = NULL;
1666
1667 sep = strchr (spec, sepc);
1668 if (sep) {
1669 if (sep - spec > 0)
1670 *addr = simple_strtoul (spec, NULL, 16);
1671
1672 *name = sep + 1;
1673 return 1;
1674 }
1675
1676 return 0;
1677}
1678
1679/**
1680 * fit_parse_conf - parse FIT configuration spec
1681 * @spec: input string, containing configuration spec
1682 * @add_curr: current image address (to be used as a possible default)
1683 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1684 * configuration
1685 * @conf_name double pointer to a char, will hold pointer to a configuration
1686 * unit name
1687 *
1688 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1689 * where <addr> is a FIT image address that contains configuration
1690 * with a <conf> unit name.
1691 *
1692 * Address part is optional, and if omitted default add_curr will
1693 * be used instead.
1694 *
1695 * returns:
1696 * 1 if spec is a valid configuration string,
1697 * addr and conf_name are set accordingly
1698 * 0 otherwise
1699 */
1700inline int fit_parse_conf (const char *spec, ulong addr_curr,
1701 ulong *addr, const char **conf_name)
1702{
1703 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1704}
1705
1706/**
1707 * fit_parse_subimage - parse FIT subimage spec
1708 * @spec: input string, containing subimage spec
1709 * @add_curr: current image address (to be used as a possible default)
1710 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1711 * subimage
1712 * @image_name: double pointer to a char, will hold pointer to a subimage name
1713 *
1714 * fit_parse_subimage() expects subimage spec in the for of
1715 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1716 * subimage with a <subimg> unit name.
1717 *
1718 * Address part is optional, and if omitted default add_curr will
1719 * be used instead.
1720 *
1721 * returns:
1722 * 1 if spec is a valid subimage string,
1723 * addr and image_name are set accordingly
1724 * 0 otherwise
1725 */
1726inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1727 ulong *addr, const char **image_name)
1728{
1729 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1730}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001731#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001732
1733static void fit_get_debug (const void *fit, int noffset,
1734 char *prop_name, int err)
1735{
1736 debug ("Can't get '%s' property from FIT 0x%08lx, "
1737 "node: offset %d, name %s (%s)\n",
1738 prop_name, (ulong)fit, noffset,
1739 fit_get_name (fit, noffset, NULL),
1740 fdt_strerror (err));
1741}
1742
1743/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001744 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001745 * @fit: pointer to the FIT format image header
1746 * @p: pointer to prefix string
1747 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001748 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001749 * The routine prints out FIT image properties (root node level) follwed by
1750 * the details of each component image.
1751 *
1752 * returns:
1753 * no returned results
1754 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001755void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001756{
1757 char *desc;
1758 char *uname;
1759 int images_noffset;
1760 int confs_noffset;
1761 int noffset;
1762 int ndepth;
1763 int count = 0;
1764 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001765 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001766#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1767 time_t timestamp;
1768#endif
1769
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001770#ifdef USE_HOSTCC
1771 p = "";
1772#else
1773 p = " ";
1774#endif
1775
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001776 /* Root node properties */
1777 ret = fit_get_desc (fit, 0, &desc);
1778 printf ("%sFIT description: ", p);
1779 if (ret)
1780 printf ("unavailable\n");
1781 else
1782 printf ("%s\n", desc);
1783
1784#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1785 ret = fit_get_timestamp (fit, 0, &timestamp);
1786 printf ("%sCreated: ", p);
1787 if (ret)
1788 printf ("unavailable\n");
1789 else
1790 genimg_print_time (timestamp);
1791#endif
1792
1793 /* Find images parent node offset */
1794 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1795 if (images_noffset < 0) {
1796 printf ("Can't find images parent node '%s' (%s)\n",
1797 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1798 return;
1799 }
1800
1801 /* Process its subnodes, print out component images details */
1802 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1803 (noffset >= 0) && (ndepth > 0);
1804 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1805 if (ndepth == 1) {
1806 /*
1807 * Direct child node of the images parent node,
1808 * i.e. component image node.
1809 */
1810 printf ("%s Image %u (%s)\n", p, count++,
1811 fit_get_name(fit, noffset, NULL));
1812
1813 fit_image_print (fit, noffset, p);
1814 }
1815 }
1816
1817 /* Find configurations parent node offset */
1818 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1819 if (confs_noffset < 0) {
1820 debug ("Can't get configurations parent node '%s' (%s)\n",
1821 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1822 return;
1823 }
1824
1825 /* get default configuration unit name from default property */
1826 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1827 if (uname)
1828 printf ("%s Default Configuration: '%s'\n", p, uname);
1829
1830 /* Process its subnodes, print out configurations details */
1831 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1832 (noffset >= 0) && (ndepth > 0);
1833 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1834 if (ndepth == 1) {
1835 /*
1836 * Direct child node of the configurations parent node,
1837 * i.e. configuration node.
1838 */
1839 printf ("%s Configuration %u (%s)\n", p, count++,
1840 fit_get_name(fit, noffset, NULL));
1841
1842 fit_conf_print (fit, noffset, p);
1843 }
1844 }
1845}
1846
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001847/**
1848 * fit_image_print - prints out the FIT component image details
1849 * @fit: pointer to the FIT format image header
1850 * @image_noffset: offset of the component image node
1851 * @p: pointer to prefix string
1852 *
1853 * fit_image_print() lists all mandatory properies for the processed component
1854 * image. If present, hash nodes are printed out as well.
1855 *
1856 * returns:
1857 * no returned results
1858 */
1859void fit_image_print (const void *fit, int image_noffset, const char *p)
1860{
1861 char *desc;
1862 uint8_t type, arch, os, comp;
1863 size_t size;
1864 ulong load, entry;
1865 const void *data;
1866 int noffset;
1867 int ndepth;
1868 int ret;
1869
1870 /* Mandatory properties */
1871 ret = fit_get_desc (fit, image_noffset, &desc);
1872 printf ("%s Description: ", p);
1873 if (ret)
1874 printf ("unavailable\n");
1875 else
1876 printf ("%s\n", desc);
1877
1878 fit_image_get_type (fit, image_noffset, &type);
1879 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1880
1881 fit_image_get_comp (fit, image_noffset, &comp);
1882 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1883
1884 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1885
1886#ifndef USE_HOSTCC
1887 printf ("%s Data Start: ", p);
1888 if (ret)
1889 printf ("unavailable\n");
1890 else
1891 printf ("0x%08lx\n", (ulong)data);
1892#endif
1893
1894 printf ("%s Data Size: ", p);
1895 if (ret)
1896 printf ("unavailable\n");
1897 else
1898 genimg_print_size (size);
1899
1900 /* Remaining, type dependent properties */
1901 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1902 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1903 (type == IH_TYPE_FLATDT)) {
1904 fit_image_get_arch (fit, image_noffset, &arch);
1905 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1906 }
1907
1908 if (type == IH_TYPE_KERNEL) {
1909 fit_image_get_os (fit, image_noffset, &os);
1910 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1911 }
1912
1913 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1914 ret = fit_image_get_load (fit, image_noffset, &load);
1915 printf ("%s Load Address: ", p);
1916 if (ret)
1917 printf ("unavailable\n");
1918 else
1919 printf ("0x%08lx\n", load);
1920
1921 fit_image_get_entry (fit, image_noffset, &entry);
1922 printf ("%s Entry Point: ", p);
1923 if (ret)
1924 printf ("unavailable\n");
1925 else
1926 printf ("0x%08lx\n", entry);
1927 }
1928
1929 /* Process all hash subnodes of the component image node */
1930 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1931 (noffset >= 0) && (ndepth > 0);
1932 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1933 if (ndepth == 1) {
1934 /* Direct child node of the component image node */
1935 fit_image_print_hash (fit, noffset, p);
1936 }
1937 }
1938}
1939
1940/**
1941 * fit_image_print_hash - prints out the hash node details
1942 * @fit: pointer to the FIT format image header
1943 * @noffset: offset of the hash node
1944 * @p: pointer to prefix string
1945 *
1946 * fit_image_print_hash() lists properies for the processed hash node
1947 *
1948 * returns:
1949 * no returned results
1950 */
1951void fit_image_print_hash (const void *fit, int noffset, const char *p)
1952{
1953 char *algo;
1954 uint8_t *value;
1955 int value_len;
1956 int i, ret;
1957
1958 /*
1959 * Check subnode name, must be equal to "hash".
1960 * Multiple hash nodes require unique unit node
1961 * names, e.g. hash@1, hash@2, etc.
1962 */
1963 if (strncmp (fit_get_name(fit, noffset, NULL),
1964 FIT_HASH_NODENAME,
1965 strlen(FIT_HASH_NODENAME)) != 0)
1966 return;
1967
1968 debug ("%s Hash node: '%s'\n", p,
1969 fit_get_name (fit, noffset, NULL));
1970
1971 printf ("%s Hash algo: ", p);
1972 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1973 printf ("invalid/unsupported\n");
1974 return;
1975 }
1976 printf ("%s\n", algo);
1977
1978 ret = fit_image_hash_get_value (fit, noffset, &value,
1979 &value_len);
1980 printf ("%s Hash value: ", p);
1981 if (ret) {
1982 printf ("unavailable\n");
1983 } else {
1984 for (i = 0; i < value_len; i++)
1985 printf ("%02x", value[i]);
1986 printf ("\n");
1987 }
1988
1989 debug ("%s Hash len: %d\n", p, value_len);
1990}
1991
1992/**
1993 * fit_get_desc - get node description property
1994 * @fit: pointer to the FIT format image header
1995 * @noffset: node offset
1996 * @desc: double pointer to the char, will hold pointer to the descrption
1997 *
1998 * fit_get_desc() reads description property from a given node, if
1999 * description is found pointer to it is returened in third call argument.
2000 *
2001 * returns:
2002 * 0, on success
2003 * -1, on failure
2004 */
2005int fit_get_desc (const void *fit, int noffset, char **desc)
2006{
2007 int len;
2008
2009 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
2010 if (*desc == NULL) {
2011 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
2012 return -1;
2013 }
2014
2015 return 0;
2016}
2017
2018/**
2019 * fit_get_timestamp - get node timestamp property
2020 * @fit: pointer to the FIT format image header
2021 * @noffset: node offset
2022 * @timestamp: pointer to the time_t, will hold read timestamp
2023 *
2024 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
2025 * is found and has a correct size its value is retured in third call
2026 * argument.
2027 *
2028 * returns:
2029 * 0, on success
2030 * -1, on property read failure
2031 * -2, on wrong timestamp size
2032 */
2033int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
2034{
2035 int len;
2036 const void *data;
2037
2038 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
2039 if (data == NULL) {
2040 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
2041 return -1;
2042 }
2043 if (len != sizeof (uint32_t)) {
2044 debug ("FIT timestamp with incorrect size of (%u)\n", len);
2045 return -2;
2046 }
2047
2048 *timestamp = uimage_to_cpu (*((uint32_t *)data));
2049 return 0;
2050}
2051
2052/**
2053 * fit_image_get_node - get node offset for component image of a given unit name
2054 * @fit: pointer to the FIT format image header
2055 * @image_uname: component image node unit name
2056 *
2057 * fit_image_get_node() finds a component image (withing the '/images'
2058 * node) of a provided unit name. If image is found its node offset is
2059 * returned to the caller.
2060 *
2061 * returns:
2062 * image node offset when found (>=0)
2063 * negative number on failure (FDT_ERR_* code)
2064 */
2065int fit_image_get_node (const void *fit, const char *image_uname)
2066{
2067 int noffset, images_noffset;
2068
2069 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2070 if (images_noffset < 0) {
2071 debug ("Can't find images parent node '%s' (%s)\n",
2072 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2073 return images_noffset;
2074 }
2075
2076 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
2077 if (noffset < 0) {
2078 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
2079 image_uname, fdt_strerror (noffset));
2080 }
2081
2082 return noffset;
2083}
2084
2085/**
2086 * fit_image_get_os - get os id for a given component image node
2087 * @fit: pointer to the FIT format image header
2088 * @noffset: component image node offset
2089 * @os: pointer to the uint8_t, will hold os numeric id
2090 *
2091 * fit_image_get_os() finds os property in a given component image node.
2092 * If the property is found, its (string) value is translated to the numeric
2093 * id which is returned to the caller.
2094 *
2095 * returns:
2096 * 0, on success
2097 * -1, on failure
2098 */
2099int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
2100{
2101 int len;
2102 const void *data;
2103
2104 /* Get OS name from property data */
2105 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
2106 if (data == NULL) {
2107 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
2108 *os = -1;
2109 return -1;
2110 }
2111
2112 /* Translate OS name to id */
2113 *os = genimg_get_os_id (data);
2114 return 0;
2115}
2116
2117/**
2118 * fit_image_get_arch - get arch id for a given component image node
2119 * @fit: pointer to the FIT format image header
2120 * @noffset: component image node offset
2121 * @arch: pointer to the uint8_t, will hold arch numeric id
2122 *
2123 * fit_image_get_arch() finds arch property in a given component image node.
2124 * If the property is found, its (string) value is translated to the numeric
2125 * id which is returned to the caller.
2126 *
2127 * returns:
2128 * 0, on success
2129 * -1, on failure
2130 */
2131int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
2132{
2133 int len;
2134 const void *data;
2135
2136 /* Get architecture name from property data */
2137 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
2138 if (data == NULL) {
2139 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
2140 *arch = -1;
2141 return -1;
2142 }
2143
2144 /* Translate architecture name to id */
2145 *arch = genimg_get_arch_id (data);
2146 return 0;
2147}
2148
2149/**
2150 * fit_image_get_type - get type id for a given component image node
2151 * @fit: pointer to the FIT format image header
2152 * @noffset: component image node offset
2153 * @type: pointer to the uint8_t, will hold type numeric id
2154 *
2155 * fit_image_get_type() finds type property in a given component image node.
2156 * If the property is found, its (string) value is translated to the numeric
2157 * id which is returned to the caller.
2158 *
2159 * returns:
2160 * 0, on success
2161 * -1, on failure
2162 */
2163int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
2164{
2165 int len;
2166 const void *data;
2167
2168 /* Get image type name from property data */
2169 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
2170 if (data == NULL) {
2171 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
2172 *type = -1;
2173 return -1;
2174 }
2175
2176 /* Translate image type name to id */
2177 *type = genimg_get_type_id (data);
2178 return 0;
2179}
2180
2181/**
2182 * fit_image_get_comp - get comp id for a given component image node
2183 * @fit: pointer to the FIT format image header
2184 * @noffset: component image node offset
2185 * @comp: pointer to the uint8_t, will hold comp numeric id
2186 *
2187 * fit_image_get_comp() finds comp property in a given component image node.
2188 * If the property is found, its (string) value is translated to the numeric
2189 * id which is returned to the caller.
2190 *
2191 * returns:
2192 * 0, on success
2193 * -1, on failure
2194 */
2195int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
2196{
2197 int len;
2198 const void *data;
2199
2200 /* Get compression name from property data */
2201 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
2202 if (data == NULL) {
2203 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
2204 *comp = -1;
2205 return -1;
2206 }
2207
2208 /* Translate compression name to id */
2209 *comp = genimg_get_comp_id (data);
2210 return 0;
2211}
2212
2213/**
2214 * fit_image_get_load - get load address property for a given component image node
2215 * @fit: pointer to the FIT format image header
2216 * @noffset: component image node offset
2217 * @load: pointer to the uint32_t, will hold load address
2218 *
2219 * fit_image_get_load() finds load address property in a given component image node.
2220 * If the property is found, its value is returned to the caller.
2221 *
2222 * returns:
2223 * 0, on success
2224 * -1, on failure
2225 */
2226int fit_image_get_load (const void *fit, int noffset, ulong *load)
2227{
2228 int len;
2229 const uint32_t *data;
2230
2231 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
2232 if (data == NULL) {
2233 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
2234 return -1;
2235 }
2236
2237 *load = uimage_to_cpu (*data);
2238 return 0;
2239}
2240
2241/**
2242 * fit_image_get_entry - get entry point address property for a given component image node
2243 * @fit: pointer to the FIT format image header
2244 * @noffset: component image node offset
2245 * @entry: pointer to the uint32_t, will hold entry point address
2246 *
2247 * fit_image_get_entry() finds entry point address property in a given component image node.
2248 * If the property is found, its value is returned to the caller.
2249 *
2250 * returns:
2251 * 0, on success
2252 * -1, on failure
2253 */
2254int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
2255{
2256 int len;
2257 const uint32_t *data;
2258
2259 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
2260 if (data == NULL) {
2261 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
2262 return -1;
2263 }
2264
2265 *entry = uimage_to_cpu (*data);
2266 return 0;
2267}
2268
2269/**
2270 * fit_image_get_data - get data property and its size for a given component image node
2271 * @fit: pointer to the FIT format image header
2272 * @noffset: component image node offset
2273 * @data: double pointer to void, will hold data property's data address
2274 * @size: pointer to size_t, will hold data property's data size
2275 *
2276 * fit_image_get_data() finds data property in a given component image node.
2277 * If the property is found its data start address and size are returned to
2278 * the caller.
2279 *
2280 * returns:
2281 * 0, on success
2282 * -1, on failure
2283 */
2284int fit_image_get_data (const void *fit, int noffset,
2285 const void **data, size_t *size)
2286{
2287 int len;
2288
2289 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
2290 if (*data == NULL) {
2291 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
2292 *size = 0;
2293 return -1;
2294 }
2295
2296 *size = len;
2297 return 0;
2298}
2299
2300/**
2301 * fit_image_hash_get_algo - get hash algorithm name
2302 * @fit: pointer to the FIT format image header
2303 * @noffset: hash node offset
2304 * @algo: double pointer to char, will hold pointer to the algorithm name
2305 *
2306 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
2307 * If the property is found its data start address is returned to the caller.
2308 *
2309 * returns:
2310 * 0, on success
2311 * -1, on failure
2312 */
2313int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
2314{
2315 int len;
2316
2317 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
2318 if (*algo == NULL) {
2319 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
2320 return -1;
2321 }
2322
2323 return 0;
2324}
2325
2326/**
2327 * fit_image_hash_get_value - get hash value and length
2328 * @fit: pointer to the FIT format image header
2329 * @noffset: hash node offset
2330 * @value: double pointer to uint8_t, will hold address of a hash value data
2331 * @value_len: pointer to an int, will hold hash data length
2332 *
2333 * fit_image_hash_get_value() finds hash value property in a given hash node.
2334 * If the property is found its data start address and size are returned to
2335 * the caller.
2336 *
2337 * returns:
2338 * 0, on success
2339 * -1, on failure
2340 */
2341int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
2342 int *value_len)
2343{
2344 int len;
2345
2346 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
2347 if (*value == NULL) {
2348 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
2349 *value_len = 0;
2350 return -1;
2351 }
2352
2353 *value_len = len;
2354 return 0;
2355}
2356
2357/**
2358 * fit_set_timestamp - set node timestamp property
2359 * @fit: pointer to the FIT format image header
2360 * @noffset: node offset
2361 * @timestamp: timestamp value to be set
2362 *
2363 * fit_set_timestamp() attempts to set timestamp property in the requested
2364 * node and returns operation status to the caller.
2365 *
2366 * returns:
2367 * 0, on success
2368 * -1, on property read failure
2369 */
2370int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
2371{
2372 uint32_t t;
2373 int ret;
2374
2375 t = cpu_to_uimage (timestamp);
2376 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
2377 sizeof (uint32_t));
2378 if (ret) {
2379 printf ("Can't set '%s' property for '%s' node (%s)\n",
2380 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
2381 fdt_strerror (ret));
2382 return -1;
2383 }
2384
2385 return 0;
2386}
2387
2388/**
2389 * calculate_hash - calculate and return hash for provided input data
2390 * @data: pointer to the input data
2391 * @data_len: data length
2392 * @algo: requested hash algorithm
2393 * @value: pointer to the char, will hold hash value data (caller must
2394 * allocate enough free space)
2395 * value_len: length of the calculated hash
2396 *
2397 * calculate_hash() computes input data hash according to the requested algorithm.
2398 * Resulting hash value is placed in caller provided 'value' buffer, length
2399 * of the calculated hash is returned via value_len pointer argument.
2400 *
2401 * returns:
2402 * 0, on success
2403 * -1, when algo is unsupported
2404 */
2405static int calculate_hash (const void *data, int data_len, const char *algo,
2406 uint8_t *value, int *value_len)
2407{
2408 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002409 *((uint32_t *)value) = crc32_wd (0, data, data_len,
2410 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002411 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
2412 *value_len = 4;
2413 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002414 sha1_csum_wd ((unsigned char *) data, data_len,
2415 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002416 *value_len = 20;
2417 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02002418 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01002419 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002420 } else {
2421 debug ("Unsupported hash alogrithm\n");
2422 return -1;
2423 }
2424 return 0;
2425}
2426
2427#ifdef USE_HOSTCC
2428/**
2429 * fit_set_hashes - process FIT component image nodes and calculate hashes
2430 * @fit: pointer to the FIT format image header
2431 *
2432 * fit_set_hashes() adds hash values for all component images in the FIT blob.
2433 * Hashes are calculated for all component images which have hash subnodes
2434 * with algorithm property set to one of the supported hash algorithms.
2435 *
2436 * returns
2437 * 0, on success
2438 * libfdt error code, on failure
2439 */
2440int fit_set_hashes (void *fit)
2441{
2442 int images_noffset;
2443 int noffset;
2444 int ndepth;
2445 int ret;
2446
2447 /* Find images parent node offset */
2448 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
2449 if (images_noffset < 0) {
2450 printf ("Can't find images parent node '%s' (%s)\n",
2451 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
2452 return images_noffset;
2453 }
2454
2455 /* Process its subnodes, print out component images details */
2456 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
2457 (noffset >= 0) && (ndepth > 0);
2458 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2459 if (ndepth == 1) {
2460 /*
2461 * Direct child node of the images parent node,
2462 * i.e. component image node.
2463 */
2464 ret = fit_image_set_hashes (fit, noffset);
2465 if (ret)
2466 return ret;
2467 }
2468 }
2469
2470 return 0;
2471}
2472
2473/**
2474 * fit_image_set_hashes - calculate/set hashes for given component image node
2475 * @fit: pointer to the FIT format image header
2476 * @image_noffset: requested component image node
2477 *
2478 * fit_image_set_hashes() adds hash values for an component image node. All
2479 * existing hash subnodes are checked, if algorithm property is set to one of
2480 * the supported hash algorithms, hash value is computed and corresponding
2481 * hash node property is set, for example:
2482 *
2483 * Input component image node structure:
2484 *
2485 * o image@1 (at image_noffset)
2486 * | - data = [binary data]
2487 * o hash@1
2488 * |- algo = "sha1"
2489 *
2490 * Output component image node structure:
2491 *
2492 * o image@1 (at image_noffset)
2493 * | - data = [binary data]
2494 * o hash@1
2495 * |- algo = "sha1"
2496 * |- value = sha1(data)
2497 *
2498 * returns:
2499 * 0 on sucess
2500 * <0 on failure
2501 */
2502int fit_image_set_hashes (void *fit, int image_noffset)
2503{
2504 const void *data;
2505 size_t size;
2506 char *algo;
2507 uint8_t value[FIT_MAX_HASH_LEN];
2508 int value_len;
2509 int noffset;
2510 int ndepth;
2511
2512 /* Get image data and data length */
2513 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2514 printf ("Can't get image data/size\n");
2515 return -1;
2516 }
2517
2518 /* Process all hash subnodes of the component image node */
2519 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2520 (noffset >= 0) && (ndepth > 0);
2521 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2522 if (ndepth == 1) {
2523 /* Direct child node of the component image node */
2524
2525 /*
2526 * Check subnode name, must be equal to "hash".
2527 * Multiple hash nodes require unique unit node
2528 * names, e.g. hash@1, hash@2, etc.
2529 */
2530 if (strncmp (fit_get_name(fit, noffset, NULL),
2531 FIT_HASH_NODENAME,
2532 strlen(FIT_HASH_NODENAME)) != 0) {
2533 /* Not a hash subnode, skip it */
2534 continue;
2535 }
2536
2537 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2538 printf ("Can't get hash algo property for "
2539 "'%s' hash node in '%s' image node\n",
2540 fit_get_name (fit, noffset, NULL),
2541 fit_get_name (fit, image_noffset, NULL));
2542 return -1;
2543 }
2544
2545 if (calculate_hash (data, size, algo, value, &value_len)) {
2546 printf ("Unsupported hash algorithm (%s) for "
2547 "'%s' hash node in '%s' image node\n",
2548 algo, fit_get_name (fit, noffset, NULL),
2549 fit_get_name (fit, image_noffset, NULL));
2550 return -1;
2551 }
2552
2553 if (fit_image_hash_set_value (fit, noffset, value,
2554 value_len)) {
2555 printf ("Can't set hash value for "
2556 "'%s' hash node in '%s' image node\n",
2557 fit_get_name (fit, noffset, NULL),
2558 fit_get_name (fit, image_noffset, NULL));
2559 return -1;
2560 }
2561 }
2562 }
2563
2564 return 0;
2565}
2566
2567/**
2568 * fit_image_hash_set_value - set hash value in requested has node
2569 * @fit: pointer to the FIT format image header
2570 * @noffset: hash node offset
2571 * @value: hash value to be set
2572 * @value_len: hash value length
2573 *
2574 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2575 * given and returns operation status to the caller.
2576 *
2577 * returns
2578 * 0, on success
2579 * -1, on failure
2580 */
2581int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2582 int value_len)
2583{
2584 int ret;
2585
2586 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2587 if (ret) {
2588 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2589 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2590 fdt_strerror (ret));
2591 return -1;
2592 }
2593
2594 return 0;
2595}
2596#endif /* USE_HOSTCC */
2597
2598/**
2599 * fit_image_check_hashes - verify data intergity
2600 * @fit: pointer to the FIT format image header
2601 * @image_noffset: component image node offset
2602 *
2603 * fit_image_check_hashes() goes over component image hash nodes,
2604 * re-calculates each data hash and compares with the value stored in hash
2605 * node.
2606 *
2607 * returns:
2608 * 1, if all hashes are valid
2609 * 0, otherwise (or on error)
2610 */
2611int fit_image_check_hashes (const void *fit, int image_noffset)
2612{
2613 const void *data;
2614 size_t size;
2615 char *algo;
2616 uint8_t *fit_value;
2617 int fit_value_len;
2618 uint8_t value[FIT_MAX_HASH_LEN];
2619 int value_len;
2620 int noffset;
2621 int ndepth;
2622 char *err_msg = "";
2623
2624 /* Get image data and data length */
2625 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2626 printf ("Can't get image data/size\n");
2627 return 0;
2628 }
2629
2630 /* Process all hash subnodes of the component image node */
2631 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2632 (noffset >= 0) && (ndepth > 0);
2633 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2634 if (ndepth == 1) {
2635 /* Direct child node of the component image node */
2636
2637 /*
2638 * Check subnode name, must be equal to "hash".
2639 * Multiple hash nodes require unique unit node
2640 * names, e.g. hash@1, hash@2, etc.
2641 */
2642 if (strncmp (fit_get_name(fit, noffset, NULL),
2643 FIT_HASH_NODENAME,
2644 strlen(FIT_HASH_NODENAME)) != 0)
2645 continue;
2646
2647 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2648 err_msg = "Can't get hash algo property";
2649 goto error;
2650 }
2651 printf ("%s", algo);
2652
2653 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2654 &fit_value_len)) {
2655 err_msg = "Can't get hash value property";
2656 goto error;
2657 }
2658
2659 if (calculate_hash (data, size, algo, value, &value_len)) {
2660 err_msg = "Unsupported hash algorithm";
2661 goto error;
2662 }
2663
2664 if (value_len != fit_value_len) {
2665 err_msg = "Bad hash value len";
2666 goto error;
2667 } else if (memcmp (value, fit_value, value_len) != 0) {
2668 err_msg = "Bad hash value";
2669 goto error;
2670 }
2671 printf ("+ ");
2672 }
2673 }
2674
2675 return 1;
2676
2677error:
2678 printf ("%s for '%s' hash node in '%s' image node\n",
2679 err_msg, fit_get_name (fit, noffset, NULL),
2680 fit_get_name (fit, image_noffset, NULL));
2681 return 0;
2682}
2683
2684/**
2685 * fit_image_check_os - check whether image node is of a given os type
2686 * @fit: pointer to the FIT format image header
2687 * @noffset: component image node offset
2688 * @os: requested image os
2689 *
2690 * fit_image_check_os() reads image os property and compares its numeric
2691 * id with the requested os. Comparison result is returned to the caller.
2692 *
2693 * returns:
2694 * 1 if image is of given os type
2695 * 0 otherwise (or on error)
2696 */
2697int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2698{
2699 uint8_t image_os;
2700
2701 if (fit_image_get_os (fit, noffset, &image_os))
2702 return 0;
2703 return (os == image_os);
2704}
2705
2706/**
2707 * fit_image_check_arch - check whether image node is of a given arch
2708 * @fit: pointer to the FIT format image header
2709 * @noffset: component image node offset
2710 * @arch: requested imagearch
2711 *
2712 * fit_image_check_arch() reads image arch property and compares its numeric
2713 * id with the requested arch. Comparison result is returned to the caller.
2714 *
2715 * returns:
2716 * 1 if image is of given arch
2717 * 0 otherwise (or on error)
2718 */
2719int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2720{
2721 uint8_t image_arch;
2722
2723 if (fit_image_get_arch (fit, noffset, &image_arch))
2724 return 0;
2725 return (arch == image_arch);
2726}
2727
2728/**
2729 * fit_image_check_type - check whether image node is of a given type
2730 * @fit: pointer to the FIT format image header
2731 * @noffset: component image node offset
2732 * @type: requested image type
2733 *
2734 * fit_image_check_type() reads image type property and compares its numeric
2735 * id with the requested type. Comparison result is returned to the caller.
2736 *
2737 * returns:
2738 * 1 if image is of given type
2739 * 0 otherwise (or on error)
2740 */
2741int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2742{
2743 uint8_t image_type;
2744
2745 if (fit_image_get_type (fit, noffset, &image_type))
2746 return 0;
2747 return (type == image_type);
2748}
2749
2750/**
2751 * fit_image_check_comp - check whether image node uses given compression
2752 * @fit: pointer to the FIT format image header
2753 * @noffset: component image node offset
2754 * @comp: requested image compression type
2755 *
2756 * fit_image_check_comp() reads image compression property and compares its
2757 * numeric id with the requested compression type. Comparison result is
2758 * returned to the caller.
2759 *
2760 * returns:
2761 * 1 if image uses requested compression
2762 * 0 otherwise (or on error)
2763 */
2764int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2765{
2766 uint8_t image_comp;
2767
2768 if (fit_image_get_comp (fit, noffset, &image_comp))
2769 return 0;
2770 return (comp == image_comp);
2771}
2772
2773/**
2774 * fit_check_format - sanity check FIT image format
2775 * @fit: pointer to the FIT format image header
2776 *
2777 * fit_check_format() runs a basic sanity FIT image verification.
2778 * Routine checks for mandatory properties, nodes, etc.
2779 *
2780 * returns:
2781 * 1, on success
2782 * 0, on failure
2783 */
2784int fit_check_format (const void *fit)
2785{
2786 /* mandatory / node 'description' property */
2787 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2788 debug ("Wrong FIT format: no description\n");
2789 return 0;
2790 }
2791
2792#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2793 /* mandatory / node 'timestamp' property */
2794 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2795 debug ("Wrong FIT format: no description\n");
2796 return 0;
2797 }
2798#endif
2799
2800 /* mandatory subimages parent '/images' node */
2801 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2802 debug ("Wrong FIT format: no images parent node\n");
2803 return 0;
2804 }
2805
2806 return 1;
2807}
2808
2809/**
2810 * fit_conf_get_node - get node offset for configuration of a given unit name
2811 * @fit: pointer to the FIT format image header
2812 * @conf_uname: configuration node unit name
2813 *
2814 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2815 * parant node) of a provided unit name. If configuration is found its node offset
2816 * is returned to the caller.
2817 *
2818 * When NULL is provided in second argument fit_conf_get_node() will search
2819 * for a default configuration node instead. Default configuration node unit name
2820 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2821 *
2822 * returns:
2823 * configuration node offset when found (>=0)
2824 * negative number on failure (FDT_ERR_* code)
2825 */
2826int fit_conf_get_node (const void *fit, const char *conf_uname)
2827{
2828 int noffset, confs_noffset;
2829 int len;
2830
2831 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2832 if (confs_noffset < 0) {
2833 debug ("Can't find configurations parent node '%s' (%s)\n",
2834 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2835 return confs_noffset;
2836 }
2837
2838 if (conf_uname == NULL) {
2839 /* get configuration unit name from the default property */
2840 debug ("No configuration specified, trying default...\n");
2841 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2842 if (conf_uname == NULL) {
2843 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2844 return len;
2845 }
2846 debug ("Found default configuration: '%s'\n", conf_uname);
2847 }
2848
2849 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2850 if (noffset < 0) {
2851 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2852 conf_uname, fdt_strerror (noffset));
2853 }
2854
2855 return noffset;
2856}
2857
2858static int __fit_conf_get_prop_node (const void *fit, int noffset,
2859 const char *prop_name)
2860{
2861 char *uname;
2862 int len;
2863
2864 /* get kernel image unit name from configuration kernel property */
2865 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2866 if (uname == NULL)
2867 return len;
2868
2869 return fit_image_get_node (fit, uname);
2870}
2871
2872/**
2873 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2874 * a given configuration
2875 * @fit: pointer to the FIT format image header
2876 * @noffset: configuration node offset
2877 *
2878 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2879 * configuration FIT_KERNEL_PROP property and translates it to the node
2880 * offset.
2881 *
2882 * returns:
2883 * image node offset when found (>=0)
2884 * negative number on failure (FDT_ERR_* code)
2885 */
2886int fit_conf_get_kernel_node (const void *fit, int noffset)
2887{
2888 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2889}
2890
2891/**
2892 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2893 * a given configuration
2894 * @fit: pointer to the FIT format image header
2895 * @noffset: configuration node offset
2896 *
2897 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2898 * configuration FIT_KERNEL_PROP property and translates it to the node
2899 * offset.
2900 *
2901 * returns:
2902 * image node offset when found (>=0)
2903 * negative number on failure (FDT_ERR_* code)
2904 */
2905int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2906{
2907 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2908}
2909
2910/**
2911 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2912 * a given configuration
2913 * @fit: pointer to the FIT format image header
2914 * @noffset: configuration node offset
2915 *
2916 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2917 * configuration FIT_KERNEL_PROP property and translates it to the node
2918 * offset.
2919 *
2920 * returns:
2921 * image node offset when found (>=0)
2922 * negative number on failure (FDT_ERR_* code)
2923 */
2924int fit_conf_get_fdt_node (const void *fit, int noffset)
2925{
2926 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2927}
2928
2929/**
2930 * fit_conf_print - prints out the FIT configuration details
2931 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01002932 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002933 * @p: pointer to prefix string
2934 *
2935 * fit_conf_print() lists all mandatory properies for the processed
2936 * configuration node.
2937 *
2938 * returns:
2939 * no returned results
2940 */
2941void fit_conf_print (const void *fit, int noffset, const char *p)
2942{
2943 char *desc;
2944 char *uname;
2945 int ret;
2946
2947 /* Mandatory properties */
2948 ret = fit_get_desc (fit, noffset, &desc);
2949 printf ("%s Description: ", p);
2950 if (ret)
2951 printf ("unavailable\n");
2952 else
2953 printf ("%s\n", desc);
2954
2955 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2956 printf ("%s Kernel: ", p);
2957 if (uname == NULL)
2958 printf ("unavailable\n");
2959 else
2960 printf ("%s\n", uname);
2961
2962 /* Optional properties */
2963 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2964 if (uname)
2965 printf ("%s Init Ramdisk: %s\n", p, uname);
2966
2967 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2968 if (uname)
2969 printf ("%s FDT: %s\n", p, uname);
2970}
Marian Balakowiczc8779642008-03-12 10:12:37 +01002971
2972/**
2973 * fit_check_ramdisk - verify FIT format ramdisk subimage
2974 * @fit_hdr: pointer to the FIT ramdisk header
2975 * @rd_noffset: ramdisk subimage node offset within FIT image
2976 * @arch: requested ramdisk image architecture type
2977 * @verify: data CRC verification flag
2978 *
2979 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2980 * specified FIT image.
2981 *
2982 * returns:
2983 * 1, on success
2984 * 0, on failure
2985 */
2986#ifndef USE_HOSTCC
2987static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2988{
2989 fit_image_print (fit, rd_noffset, " ");
2990
2991 if (verify) {
2992 puts (" Verifying Hash Integrity ... ");
2993 if (!fit_image_check_hashes (fit, rd_noffset)) {
2994 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01002995 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01002996 return 0;
2997 }
2998 puts ("OK\n");
2999 }
3000
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003001 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003002 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
3003 !fit_image_check_arch (fit, rd_noffset, arch) ||
3004 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
3005 printf ("No Linux %s Ramdisk Image\n",
3006 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003007 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003008 return 0;
3009 }
3010
Marian Balakowicz1372cce2008-03-12 10:33:01 +01003011 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01003012 return 1;
3013}
3014#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01003015#endif /* CONFIG_FIT */