blob: 051d298050fa7d87afe8c0b0367b0bda85b97f21 [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 Balakowiczceaed2b2008-01-31 13:57:17 +010026
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010027#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010028#include <common.h>
29#include <watchdog.h>
30
31#ifdef CONFIG_SHOW_BOOT_PROGRESS
32#include <status_led.h>
33#endif
34
35#ifdef CONFIG_HAS_DATAFLASH
36#include <dataflash.h>
37#endif
38
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010039#ifdef CONFIG_LOGBUFFER
40#include <logbuff.h>
41#endif
42
Marian Balakowicz2242f532008-02-21 17:27:41 +010043#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
44#include <rtc.h>
45#endif
46
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010047#include <image.h>
48
Marian Balakowiczc8779642008-03-12 10:12:37 +010049#if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +010050#include <fdt.h>
51#include <libfdt.h>
52#include <fdt_support.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010053#endif
54
55#if defined(CONFIG_FIT)
Andy Fleming20a14a42008-04-02 16:19:07 -050056#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010057#include <sha1.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010058
59static int fit_check_ramdisk (const void *fit, int os_noffset,
60 uint8_t arch, int verify);
Marian Balakowiczfff888a12008-02-21 17:20:19 +010061#endif
62
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010063#ifdef CONFIG_CMD_BDI
64extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
65#endif
66
67DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010068
Marian Balakowiczd985c842008-03-12 10:14:38 +010069static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
70 int verify);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010071#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010072#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050073#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010074#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010075#include <image.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010076#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010077
Marian Balakowicz570abb02008-02-29 15:59:59 +010078typedef struct table_entry {
79 int id; /* as defined in image.h */
80 char *sname; /* short (input) name */
81 char *lname; /* long (output) name */
82} table_entry_t;
83
84static table_entry_t uimage_arch[] = {
85 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
86 { IH_ARCH_ALPHA, "alpha", "Alpha", },
87 { IH_ARCH_ARM, "arm", "ARM", },
88 { IH_ARCH_I386, "x86", "Intel x86", },
89 { IH_ARCH_IA64, "ia64", "IA64", },
90 { IH_ARCH_M68K, "m68k", "M68K", },
91 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
92 { IH_ARCH_MIPS, "mips", "MIPS", },
93 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
94 { IH_ARCH_NIOS, "nios", "NIOS", },
95 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070096 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010097 { IH_ARCH_PPC, "ppc", "PowerPC", },
98 { IH_ARCH_S390, "s390", "IBM S390", },
99 { IH_ARCH_SH, "sh", "SuperH", },
100 { IH_ARCH_SPARC, "sparc", "SPARC", },
101 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
102 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
103 { IH_ARCH_AVR32, "avr32", "AVR32", },
104 { -1, "", "", },
105};
106
107static table_entry_t uimage_os[] = {
108 { IH_OS_INVALID, NULL, "Invalid OS", },
109#if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
110 { IH_OS_ARTOS, "artos", "ARTOS", },
111#endif
112 { IH_OS_LINUX, "linux", "Linux", },
113#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
114 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
115#endif
116 { IH_OS_NETBSD, "netbsd", "NetBSD", },
117 { IH_OS_RTEMS, "rtems", "RTEMS", },
118 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
119#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
120 { IH_OS_QNX, "qnx", "QNX", },
121 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
122#endif
123#ifdef USE_HOSTCC
124 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
125 { IH_OS_DELL, "dell", "Dell", },
126 { IH_OS_ESIX, "esix", "Esix", },
127 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
128 { IH_OS_IRIX, "irix", "Irix", },
129 { IH_OS_NCR, "ncr", "NCR", },
130 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
131 { IH_OS_PSOS, "psos", "pSOS", },
132 { IH_OS_SCO, "sco", "SCO", },
133 { IH_OS_SOLARIS, "solaris", "Solaris", },
134 { IH_OS_SVR4, "svr4", "SVR4", },
135#endif
136 { -1, "", "", },
137};
138
139static table_entry_t uimage_type[] = {
140 { IH_TYPE_INVALID, NULL, "Invalid Image", },
141 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
142 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
143 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
144 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
145 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
146 { IH_TYPE_SCRIPT, "script", "Script", },
147 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
148 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
149 { -1, "", "", },
150};
151
152static table_entry_t uimage_comp[] = {
153 { IH_COMP_NONE, "none", "uncompressed", },
154 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
155 { IH_COMP_GZIP, "gzip", "gzip compressed", },
156 { -1, "", "", },
157};
158
Mike Frysinger89cdab72008-03-31 11:02:01 -0400159uint32_t crc32 (uint32_t, const unsigned char *, uint);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200160uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100161static void genimg_print_size (uint32_t size);
162#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
163static void genimg_print_time (time_t timestamp);
164#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100165
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100166/*****************************************************************************/
167/* Legacy format routines */
168/*****************************************************************************/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100169int image_check_hcrc (image_header_t *hdr)
170{
171 ulong hcrc;
172 ulong len = image_get_header_size ();
173 image_header_t header;
174
175 /* Copy header so we can blank CRC field for re-calculation */
176 memmove (&header, (char *)hdr, image_get_header_size ());
177 image_set_hcrc (&header, 0);
178
179 hcrc = crc32 (0, (unsigned char *)&header, len);
180
181 return (hcrc == image_get_hcrc (hdr));
182}
183
184int image_check_dcrc (image_header_t *hdr)
185{
186 ulong data = image_get_data (hdr);
187 ulong len = image_get_data_size (hdr);
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200188 ulong dcrc = crc32_wd (0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100189
190 return (dcrc == image_get_dcrc (hdr));
191}
192
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100193
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100194/**
195 * image_multi_count - get component (sub-image) count
196 * @hdr: pointer to the header of the multi component image
197 *
198 * image_multi_count() returns number of components in a multi
199 * component image.
200 *
201 * Note: no checking of the image type is done, caller must pass
202 * a valid multi component image.
203 *
204 * returns:
205 * number of components
206 */
207ulong image_multi_count (image_header_t *hdr)
208{
209 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100210 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100211
212 /* get start of the image payload, which in case of multi
213 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100214 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100215
216 /* count non empty slots */
217 for (i = 0; size[i]; ++i)
218 count++;
219
220 return count;
221}
222
223/**
224 * image_multi_getimg - get component data address and size
225 * @hdr: pointer to the header of the multi component image
226 * @idx: index of the requested component
227 * @data: pointer to a ulong variable, will hold component data address
228 * @len: pointer to a ulong variable, will hold component size
229 *
230 * image_multi_getimg() returns size and data address for the requested
231 * component in a multi component image.
232 *
233 * Note: no checking of the image type is done, caller must pass
234 * a valid multi component image.
235 *
236 * returns:
237 * data address and size of the component, if idx is valid
238 * 0 in data and len, if idx is out of range
239 */
240void image_multi_getimg (image_header_t *hdr, ulong idx,
241 ulong *data, ulong *len)
242{
243 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100244 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100245 ulong offset, tail, count, img_data;
246
247 /* get number of component */
248 count = image_multi_count (hdr);
249
250 /* get start of the image payload, which in case of multi
251 * component images that points to a table of component sizes */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100252 size = (uint32_t *)image_get_data (hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100253
254 /* get address of the proper component data start, which means
255 * skipping sizes table (add 1 for last, null entry) */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100256 img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100257
258 if (idx < count) {
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100259 *len = uimage_to_cpu (size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100260 offset = 0;
261 tail = 0;
262
263 /* go over all indices preceding requested component idx */
264 for (i = 0; i < idx; i++) {
265 /* add up i-th component size */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100266 offset += uimage_to_cpu (size[i]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100267
268 /* add up alignment for i-th component */
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100269 tail += (4 - uimage_to_cpu (size[i]) % 4);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100270 }
271
272 /* calculate idx-th component data address */
273 *data = img_data + offset + tail;
274 } else {
275 *len = 0;
276 *data = 0;
277 }
278}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100279
Marian Balakowicz2242f532008-02-21 17:27:41 +0100280static void image_print_type (image_header_t *hdr)
281{
282 const char *os, *arch, *type, *comp;
283
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100284 os = genimg_get_os_name (image_get_os (hdr));
285 arch = genimg_get_arch_name (image_get_arch (hdr));
286 type = genimg_get_type_name (image_get_type (hdr));
287 comp = genimg_get_comp_name (image_get_comp (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100288
Marian Balakowicz570abb02008-02-29 15:59:59 +0100289 printf ("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100290}
291
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100292/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200293 * image_print_contents - prints out the contents of the legacy format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100294 * @hdr: pointer to the legacy format image header
295 * @p: pointer to prefix string
296 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200297 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100298 * The routine prints out all header fields followed by the size/offset data
299 * for MULTI/SCRIPT images.
300 *
301 * returns:
302 * no returned results
303 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200304void image_print_contents (image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100305{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200306 const char *p;
307
308#ifdef USE_HOSTCC
309 p = "";
310#else
311 p = " ";
312#endif
313
Marian Balakowicz570abb02008-02-29 15:59:59 +0100314 printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
315#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
316 printf ("%sCreated: ", p);
317 genimg_print_time ((time_t)image_get_time (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100318#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100319 printf ("%sImage Type: ", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100320 image_print_type (hdr);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100321 printf ("%sData Size: ", p);
322 genimg_print_size (image_get_data_size (hdr));
323 printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
324 printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100325
Marian Balakowicz570abb02008-02-29 15:59:59 +0100326 if (image_check_type (hdr, IH_TYPE_MULTI) ||
327 image_check_type (hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100328 int i;
329 ulong data, len;
330 ulong count = image_multi_count (hdr);
331
Marian Balakowicz570abb02008-02-29 15:59:59 +0100332 printf ("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100333 for (i = 0; i < count; i++) {
334 image_multi_getimg (hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100335
336 printf ("%s Image %d: ", p, i);
337 genimg_print_size (len);
338
339 if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
340 /*
341 * the user may need to know offsets
342 * if planning to do something with
343 * multiple files
344 */
345 printf ("%s Offset = 0x%08lx\n", p, data);
346 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100347 }
348 }
349}
350
Marian Balakowicz570abb02008-02-29 15:59:59 +0100351
352#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100353/**
354 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100355 * @rd_addr: ramdisk image start address
356 * @arch: expected ramdisk architecture
357 * @verify: checksum verification flag
358 *
359 * image_get_ramdisk() returns a pointer to the verified ramdisk image
360 * header. Routine receives image start address and expected architecture
361 * flag. Verification done covers data and header integrity and os/type/arch
362 * fields checking.
363 *
364 * If dataflash support is enabled routine checks for dataflash addresses
365 * and handles required dataflash reads.
366 *
367 * returns:
368 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600369 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100370 */
Marian Balakowiczd985c842008-03-12 10:14:38 +0100371static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
372 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100373{
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100374 image_header_t *rd_hdr = (image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100375
376 if (!image_check_magic (rd_hdr)) {
377 puts ("Bad Magic Number\n");
378 show_boot_progress (-10);
Kumar Gala274cea22008-02-27 21:51:46 -0600379 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100380 }
381
382 if (!image_check_hcrc (rd_hdr)) {
383 puts ("Bad Header Checksum\n");
384 show_boot_progress (-11);
Kumar Gala274cea22008-02-27 21:51:46 -0600385 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100386 }
387
388 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100389 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100390
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100391 if (verify) {
392 puts(" Verifying Checksum ... ");
Bartlomiej Sieka75903782008-04-25 13:54:02 +0200393 if (!image_check_dcrc (rd_hdr)) {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100394 puts ("Bad Data CRC\n");
395 show_boot_progress (-12);
Kumar Gala274cea22008-02-27 21:51:46 -0600396 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100397 }
398 puts("OK\n");
399 }
400
401 show_boot_progress (11);
402
403 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
404 !image_check_arch (rd_hdr, arch) ||
405 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
406 printf ("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100407 genimg_get_arch_name(arch));
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100408 show_boot_progress (-13);
Kumar Gala274cea22008-02-27 21:51:46 -0600409 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100410 }
411
412 return rd_hdr;
413}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100414#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100415
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100416/*****************************************************************************/
417/* Shared dual-format routines */
418/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100419#ifndef USE_HOSTCC
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200420int getenv_yesno (char *var)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100421{
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200422 char *s = getenv (var);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100423 return (s && (*s == 'n')) ? 0 : 1;
424}
425
426ulong getenv_bootm_low(void)
427{
428 char *s = getenv ("bootm_low");
429 if (s) {
430 ulong tmp = simple_strtoul (s, NULL, 16);
431 return tmp;
432 }
433
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100434#if defined(CFG_SDRAM_BASE)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100435 return CFG_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100436#elif defined(CONFIG_ARM)
437 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100438#else
439 return 0;
440#endif
441}
442
443ulong getenv_bootm_size(void)
444{
445 char *s = getenv ("bootm_size");
446 if (s) {
447 ulong tmp = simple_strtoul (s, NULL, 16);
448 return tmp;
449 }
450
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100451#if defined(CONFIG_ARM)
452 return gd->bd->bi_dram[0].size;
453#else
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100454 return gd->bd->bi_memsize;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100455#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100456}
457
458void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
459{
460#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
461 while (len > 0) {
462 size_t tail = (len > chunksz) ? chunksz : len;
463 WATCHDOG_RESET ();
464 memmove (to, from, tail);
465 to += tail;
466 from += tail;
467 len -= tail;
468 }
469#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
470 memmove (to, from, len);
471#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
472}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100473#endif /* !USE_HOSTCC */
474
475static void genimg_print_size (uint32_t size)
476{
477#ifndef USE_HOSTCC
478 printf ("%d Bytes = ", size);
479 print_size (size, "\n");
480#else
481 printf ("%d Bytes = %.2f kB = %.2f MB\n",
482 size, (double)size / 1.024e3,
483 (double)size / 1.048576e6);
484#endif
485}
486
487#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
488static void genimg_print_time (time_t timestamp)
489{
490#ifndef USE_HOSTCC
491 struct rtc_time tm;
492
493 to_tm (timestamp, &tm);
494 printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
495 tm.tm_year, tm.tm_mon, tm.tm_mday,
496 tm.tm_hour, tm.tm_min, tm.tm_sec);
497#else
498 printf ("%s", ctime(&timestamp));
499#endif
500}
501#endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
502
503/**
504 * get_table_entry_name - translate entry id to long name
505 * @table: pointer to a translation table for entries of a specific type
506 * @msg: message to be returned when translation fails
507 * @id: entry id to be translated
508 *
509 * get_table_entry_name() will go over translation table trying to find
510 * entry that matches given id. If matching entry is found, its long
511 * name is returned to the caller.
512 *
513 * returns:
514 * long entry name if translation succeeds
515 * msg otherwise
516 */
517static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
518{
519 for (; table->id >= 0; ++table) {
520 if (table->id == id)
521 return (table->lname);
522 }
523 return (msg);
524}
525
526const char *genimg_get_os_name (uint8_t os)
527{
528 return (get_table_entry_name (uimage_os, "Unknown OS", os));
529}
530
531const char *genimg_get_arch_name (uint8_t arch)
532{
533 return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
534}
535
536const char *genimg_get_type_name (uint8_t type)
537{
538 return (get_table_entry_name (uimage_type, "Unknown Image", type));
539}
540
541const char *genimg_get_comp_name (uint8_t comp)
542{
543 return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
544}
545
546/**
547 * get_table_entry_id - translate short entry name to id
548 * @table: pointer to a translation table for entries of a specific type
549 * @table_name: to be used in case of error
550 * @name: entry short name to be translated
551 *
552 * get_table_entry_id() will go over translation table trying to find
553 * entry that matches given short name. If matching entry is found,
554 * its id returned to the caller.
555 *
556 * returns:
557 * entry id if translation succeeds
558 * -1 otherwise
559 */
560static int get_table_entry_id (table_entry_t *table,
561 const char *table_name, const char *name)
562{
563 table_entry_t *t;
564#ifdef USE_HOSTCC
565 int first = 1;
566
567 for (t = table; t->id >= 0; ++t) {
568 if (t->sname && strcasecmp(t->sname, name) == 0)
569 return (t->id);
570 }
571
572 fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
573 for (t = table; t->id >= 0; ++t) {
574 if (t->sname == NULL)
575 continue;
576 fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
577 first = 0;
578 }
579 fprintf (stderr, "\n");
580#else
581 for (t = table; t->id >= 0; ++t) {
582 if (t->sname && strcmp(t->sname, name) == 0)
583 return (t->id);
584 }
585 debug ("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100586#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100587 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100588}
589
Marian Balakowicz570abb02008-02-29 15:59:59 +0100590int genimg_get_os_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100591{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100592 return (get_table_entry_id (uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100593}
594
Marian Balakowicz570abb02008-02-29 15:59:59 +0100595int genimg_get_arch_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100596{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100597 return (get_table_entry_id (uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100598}
599
Marian Balakowicz570abb02008-02-29 15:59:59 +0100600int genimg_get_type_id (const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100601{
Marian Balakowicz570abb02008-02-29 15:59:59 +0100602 return (get_table_entry_id (uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100603}
604
Marian Balakowicz570abb02008-02-29 15:59:59 +0100605int genimg_get_comp_id (const char *name)
606{
607 return (get_table_entry_id (uimage_comp, "Compression", name));
608}
609
610#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100611/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100612 * genimg_get_format - get image format type
613 * @img_addr: image start address
614 *
615 * genimg_get_format() checks whether provided address points to a valid
616 * legacy or FIT image.
617 *
618 * New uImage format and FDT blob are based on a libfdt. FDT blob
619 * may be passed directly or embedded in a FIT image. In both situations
620 * genimg_get_format() must be able to dectect libfdt header.
621 *
622 * returns:
623 * image format type or IMAGE_FORMAT_INVALID if no image is present
624 */
625int genimg_get_format (void *img_addr)
626{
627 ulong format = IMAGE_FORMAT_INVALID;
628 image_header_t *hdr;
629#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
630 char *fit_hdr;
631#endif
632
633 hdr = (image_header_t *)img_addr;
634 if (image_check_magic(hdr))
635 format = IMAGE_FORMAT_LEGACY;
636#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
637 else {
638 fit_hdr = (char *)img_addr;
639 if (fdt_check_header (fit_hdr) == 0)
640 format = IMAGE_FORMAT_FIT;
641 }
642#endif
643
644 return format;
645}
646
647/**
648 * genimg_get_image - get image from special storage (if necessary)
649 * @img_addr: image start address
650 *
651 * genimg_get_image() checks if provided image start adddress is located
652 * in a dataflash storage. If so, image is moved to a system RAM memory.
653 *
654 * returns:
655 * image start address after possible relocation from special storage
656 */
657ulong genimg_get_image (ulong img_addr)
658{
659 ulong ram_addr = img_addr;
660
661#ifdef CONFIG_HAS_DATAFLASH
662 ulong h_size, d_size;
663
664 if (addr_dataflash (img_addr)){
665 /* ger RAM address */
666 ram_addr = CFG_LOAD_ADDR;
667
668 /* get header size */
669 h_size = image_get_header_size ();
670#if defined(CONFIG_FIT)
671 if (sizeof(struct fdt_header) > h_size)
672 h_size = sizeof(struct fdt_header);
673#endif
674
675 /* read in header */
676 debug (" Reading image header from dataflash address "
677 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
678
679 read_dataflash (img_addr, h_size, (char *)ram_addr);
680
681 /* get data size */
682 switch (genimg_get_format ((void *)ram_addr)) {
683 case IMAGE_FORMAT_LEGACY:
684 d_size = image_get_data_size ((image_header_t *)ram_addr);
685 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
686 ram_addr, d_size);
687 break;
688#if defined(CONFIG_FIT)
689 case IMAGE_FORMAT_FIT:
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100690 d_size = fit_get_size ((const void *)ram_addr) - h_size;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100691 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
692 ram_addr, d_size);
693 break;
694#endif
695 default:
696 printf (" No valid image found at 0x%08lx\n", img_addr);
697 return ram_addr;
698 }
699
700 /* read in image data */
701 debug (" Reading image remaining data from dataflash address "
702 "%08lx to RAM address %08lx\n", img_addr + h_size,
703 ram_addr + h_size);
704
705 read_dataflash (img_addr + h_size, d_size,
706 (char *)(ram_addr + h_size));
707
708 }
709#endif /* CONFIG_HAS_DATAFLASH */
710
711 return ram_addr;
712}
713
714/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100715 * fit_has_config - check if there is a valid FIT configuration
716 * @images: pointer to the bootm command headers structure
717 *
718 * fit_has_config() checks if there is a FIT configuration in use
719 * (if FTI support is present).
720 *
721 * returns:
722 * 0, no FIT support or no configuration found
723 * 1, configuration found
724 */
725int genimg_has_config (bootm_headers_t *images)
726{
727#if defined(CONFIG_FIT)
728 if (images->fit_uname_cfg)
729 return 1;
730#endif
731 return 0;
732}
733
734/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100735 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100736 * @argc: command argument count
737 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100738 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100739 * @arch: expected ramdisk architecture
740 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
741 * @rd_end: pointer to a ulong variable, will hold ramdisk end
742 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100743 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100744 * Curently supported are the following ramdisk sources:
745 * - multicomponent kernel/ramdisk image,
746 * - commandline provided address of decicated ramdisk image.
747 *
748 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100749 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100750 * rd_start and rd_end are set to ramdisk start/end addresses if
751 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100752 *
753 * 1, if ramdisk image is found but corrupted
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100754 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100755 */
Marian Balakowiczd985c842008-03-12 10:14:38 +0100756int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
757 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100758{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100759 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100760 ulong rd_data, rd_len;
761 image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100762#if defined(CONFIG_FIT)
763 void *fit_hdr;
764 const char *fit_uname_config = NULL;
765 const char *fit_uname_ramdisk = NULL;
766 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100767 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100768 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100769 const void *data;
770 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100771#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100772
Marian Balakowiczc8779642008-03-12 10:12:37 +0100773 *rd_start = 0;
774 *rd_end = 0;
775
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100776 /*
777 * Look for a '-' which indicates to ignore the
778 * ramdisk argument
779 */
780 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
781 debug ("## Skipping init Ramdisk\n");
782 rd_len = rd_data = 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100783 } else if (argc >= 3 || genimg_has_config (images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100784#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100785 if (argc >= 3) {
786 /*
787 * If the init ramdisk comes from the FIT image and
788 * the FIT image address is omitted in the command
789 * line argument, try to use os FIT image address or
790 * default load address.
791 */
792 if (images->fit_uname_os)
793 default_addr = (ulong)images->fit_hdr_os;
794 else
795 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100796
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100797 if (fit_parse_conf (argv[2], default_addr,
798 &rd_addr, &fit_uname_config)) {
799 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
800 fit_uname_config, rd_addr);
801 } else if (fit_parse_subimage (argv[2], default_addr,
802 &rd_addr, &fit_uname_ramdisk)) {
803 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
804 fit_uname_ramdisk, rd_addr);
805 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100806#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100807 {
808 rd_addr = simple_strtoul(argv[2], NULL, 16);
809 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
810 rd_addr);
811 }
812#if defined(CONFIG_FIT)
813 } else {
814 /* use FIT configuration provided in first bootm
815 * command argument
816 */
817 rd_addr = (ulong)images->fit_hdr_os;
818 fit_uname_config = images->fit_uname_cfg;
819 debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
820 fit_uname_config, rd_addr);
821
822 /*
823 * Check whether configuration has ramdisk defined,
824 * if not, don't try to use it, quit silently.
825 */
826 fit_hdr = (void *)rd_addr;
827 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
828 if (cfg_noffset < 0) {
829 debug ("* ramdisk: no such config\n");
830 return 0;
831 }
832
833 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
834 if (rd_noffset < 0) {
835 debug ("* ramdisk: no ramdisk in config\n");
836 return 0;
837 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100838 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100839#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100840
841 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100842 rd_addr = genimg_get_image (rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100843
844 /*
845 * Check if there is an initrd image at the
846 * address provided in the second bootm argument
847 * check image type, for FIT images get FIT node.
848 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100849 switch (genimg_get_format ((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100850 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczc8779642008-03-12 10:12:37 +0100851 printf ("## Loading init Ramdisk from Legacy "
852 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100853
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100854 show_boot_progress (9);
Marian Balakowiczd985c842008-03-12 10:14:38 +0100855 rd_hdr = image_get_ramdisk (rd_addr, arch,
856 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100857
Marian Balakowiczc8779642008-03-12 10:12:37 +0100858 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600859 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600860
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100861 rd_data = image_get_data (rd_hdr);
862 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100863 rd_load = image_get_load (rd_hdr);
864 break;
865#if defined(CONFIG_FIT)
866 case IMAGE_FORMAT_FIT:
867 fit_hdr = (void *)rd_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100868 printf ("## Loading init Ramdisk from FIT "
869 "Image at %08lx ...\n", rd_addr);
870
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100871 show_boot_progress (120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100872 if (!fit_check_format (fit_hdr)) {
873 puts ("Bad FIT ramdisk image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100874 show_boot_progress (-120);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100875 return 0;
876 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100877 show_boot_progress (121);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100878
879 if (!fit_uname_ramdisk) {
880 /*
881 * no ramdisk image node unit name, try to get config
882 * node first. If config unit node name is NULL
883 * fit_conf_get_node() will try to find default config node
884 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100885 show_boot_progress (122);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100886 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
887 if (cfg_noffset < 0) {
888 puts ("Could not find configuration node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100889 show_boot_progress (-122);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100890 return 0;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100891 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100892 fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
893 printf (" Using '%s' configuration\n", fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100894
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100895 rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100896 fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
897 } else {
898 /* get ramdisk component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100899 show_boot_progress (123);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100900 rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
901 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100902 if (rd_noffset < 0) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100903 puts ("Could not find subimage node\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100904 show_boot_progress (-124);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100905 return 0;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100906 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100907
908 printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
909
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100910 show_boot_progress (125);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100911 if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
912 return 0;
913
914 /* get ramdisk image data address and length */
915 if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
916 puts ("Could not find ramdisk subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100917 show_boot_progress (-127);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100918 return 0;
919 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100920 show_boot_progress (128);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100921
922 rd_data = (ulong)data;
923 rd_len = size;
924
925 if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
926 puts ("Can't get ramdisk subimage load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100927 show_boot_progress (-129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100928 return 0;
929 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100930 show_boot_progress (129);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100931
932 images->fit_hdr_rd = fit_hdr;
933 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100934 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100935 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100936#endif
937 default:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100938 puts ("Wrong Ramdisk Image Format\n");
Marian Balakowiczc8779642008-03-12 10:12:37 +0100939 rd_data = rd_len = rd_load = 0;
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 Balakowiczceaed2b2008-01-31 13:57:17 +01001025 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1026 initrd_high, initrd_copy_to_ram);
1027
1028 if (rd_data) {
1029 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
1030 debug (" in-place initrd\n");
1031 *initrd_start = rd_data;
1032 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001033 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001034 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001035 if (initrd_high)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001036 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001037 else
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001038 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001039
Kumar Galae822d7f2008-02-27 21:51:49 -06001040 if (*initrd_start == 0) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001041 puts ("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001042 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001043 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001044 show_boot_progress (12);
1045
1046 *initrd_end = *initrd_start + rd_len;
1047 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
1048 *initrd_start, *initrd_end);
1049
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001050 memmove_wd ((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001051 (void *)rd_data, rd_len, CHUNKSZ);
1052
1053 puts ("OK\n");
1054 }
1055 } else {
1056 *initrd_start = 0;
1057 *initrd_end = 0;
1058 }
1059 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1060 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001061
Kumar Galae822d7f2008-02-27 21:51:49 -06001062 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001063
Kumar Galae822d7f2008-02-27 21:51:49 -06001064error:
1065 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001066}
1067
1068/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001069 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001070 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001071 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1072 * @cmd_end: pointer to a ulong variable, will hold cmdline end
Kumar Galae822d7f2008-02-27 21:51:49 -06001073 * @bootmap_base: ulong variable, holds offset in physical memory to
1074 * base of bootmap
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001075 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001076 * boot_get_cmdline() allocates space for kernel command line below
Kumar Galae822d7f2008-02-27 21:51:49 -06001077 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001078 * variable is present its contents is copied to allocated kernel
1079 * command line.
1080 *
1081 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001082 * 0 - success
1083 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001084 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001085int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
Kumar Galae822d7f2008-02-27 21:51:49 -06001086 ulong bootmap_base)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001087{
1088 char *cmdline;
1089 char *s;
1090
Kumar Galae822d7f2008-02-27 21:51:49 -06001091 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
1092 CFG_BOOTMAPSZ + bootmap_base);
1093
1094 if (cmdline == NULL)
1095 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001096
1097 if ((s = getenv("bootargs")) == NULL)
1098 s = "";
1099
1100 strcpy(cmdline, s);
1101
1102 *cmd_start = (ulong) & cmdline[0];
1103 *cmd_end = *cmd_start + strlen(cmdline);
1104
1105 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1106
Kumar Galae822d7f2008-02-27 21:51:49 -06001107 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001108}
1109
1110/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001111 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001112 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001113 * @kbd: double pointer to board info data
Kumar Galae822d7f2008-02-27 21:51:49 -06001114 * @bootmap_base: ulong variable, holds offset in physical memory to
1115 * base of bootmap
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001116 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001117 * boot_get_kbd() allocates space for kernel copy of board info data below
Kumar Galae822d7f2008-02-27 21:51:49 -06001118 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
1119 * the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001120 *
1121 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001122 * 0 - success
1123 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001124 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001125int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001126{
Kumar Galae822d7f2008-02-27 21:51:49 -06001127 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1128 CFG_BOOTMAPSZ + bootmap_base);
1129 if (*kbd == NULL)
1130 return -1;
1131
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001132 **kbd = *(gd->bd);
1133
1134 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1135
1136#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1137 do_bdinfo(NULL, 0, 0, NULL);
1138#endif
1139
Kumar Galae822d7f2008-02-27 21:51:49 -06001140 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001141}
1142#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001143#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001144
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001145#if defined(CONFIG_FIT)
1146/*****************************************************************************/
1147/* New uImage format routines */
1148/*****************************************************************************/
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001149#ifndef USE_HOSTCC
Marian Balakowiczf50433d2008-02-21 17:20:20 +01001150static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
1151 ulong *addr, const char **name)
1152{
1153 const char *sep;
1154
1155 *addr = addr_curr;
1156 *name = NULL;
1157
1158 sep = strchr (spec, sepc);
1159 if (sep) {
1160 if (sep - spec > 0)
1161 *addr = simple_strtoul (spec, NULL, 16);
1162
1163 *name = sep + 1;
1164 return 1;
1165 }
1166
1167 return 0;
1168}
1169
1170/**
1171 * fit_parse_conf - parse FIT configuration spec
1172 * @spec: input string, containing configuration spec
1173 * @add_curr: current image address (to be used as a possible default)
1174 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1175 * configuration
1176 * @conf_name double pointer to a char, will hold pointer to a configuration
1177 * unit name
1178 *
1179 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
1180 * where <addr> is a FIT image address that contains configuration
1181 * with a <conf> unit name.
1182 *
1183 * Address part is optional, and if omitted default add_curr will
1184 * be used instead.
1185 *
1186 * returns:
1187 * 1 if spec is a valid configuration string,
1188 * addr and conf_name are set accordingly
1189 * 0 otherwise
1190 */
1191inline int fit_parse_conf (const char *spec, ulong addr_curr,
1192 ulong *addr, const char **conf_name)
1193{
1194 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
1195}
1196
1197/**
1198 * fit_parse_subimage - parse FIT subimage spec
1199 * @spec: input string, containing subimage spec
1200 * @add_curr: current image address (to be used as a possible default)
1201 * @addr: pointer to a ulong variable, will hold FIT image address of a given
1202 * subimage
1203 * @image_name: double pointer to a char, will hold pointer to a subimage name
1204 *
1205 * fit_parse_subimage() expects subimage spec in the for of
1206 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
1207 * subimage with a <subimg> unit name.
1208 *
1209 * Address part is optional, and if omitted default add_curr will
1210 * be used instead.
1211 *
1212 * returns:
1213 * 1 if spec is a valid subimage string,
1214 * addr and image_name are set accordingly
1215 * 0 otherwise
1216 */
1217inline int fit_parse_subimage (const char *spec, ulong addr_curr,
1218 ulong *addr, const char **image_name)
1219{
1220 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
1221}
Marian Balakowicz570abb02008-02-29 15:59:59 +01001222#endif /* !USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001223
1224static void fit_get_debug (const void *fit, int noffset,
1225 char *prop_name, int err)
1226{
1227 debug ("Can't get '%s' property from FIT 0x%08lx, "
1228 "node: offset %d, name %s (%s)\n",
1229 prop_name, (ulong)fit, noffset,
1230 fit_get_name (fit, noffset, NULL),
1231 fdt_strerror (err));
1232}
1233
1234/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001235 * fit_print_contents - prints out the contents of the FIT format image
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001236 * @fit: pointer to the FIT format image header
1237 * @p: pointer to prefix string
1238 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001239 * fit_print_contents() formats a multi line FIT image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001240 * The routine prints out FIT image properties (root node level) follwed by
1241 * the details of each component image.
1242 *
1243 * returns:
1244 * no returned results
1245 */
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001246void fit_print_contents (const void *fit)
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001247{
1248 char *desc;
1249 char *uname;
1250 int images_noffset;
1251 int confs_noffset;
1252 int noffset;
1253 int ndepth;
1254 int count = 0;
1255 int ret;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001256 const char *p;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001257#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1258 time_t timestamp;
1259#endif
1260
Bartlomiej Siekaedbed242008-04-18 12:39:23 +02001261#ifdef USE_HOSTCC
1262 p = "";
1263#else
1264 p = " ";
1265#endif
1266
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001267 /* Root node properties */
1268 ret = fit_get_desc (fit, 0, &desc);
1269 printf ("%sFIT description: ", p);
1270 if (ret)
1271 printf ("unavailable\n");
1272 else
1273 printf ("%s\n", desc);
1274
1275#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
1276 ret = fit_get_timestamp (fit, 0, &timestamp);
1277 printf ("%sCreated: ", p);
1278 if (ret)
1279 printf ("unavailable\n");
1280 else
1281 genimg_print_time (timestamp);
1282#endif
1283
1284 /* Find images parent node offset */
1285 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1286 if (images_noffset < 0) {
1287 printf ("Can't find images parent node '%s' (%s)\n",
1288 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1289 return;
1290 }
1291
1292 /* Process its subnodes, print out component images details */
1293 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1294 (noffset >= 0) && (ndepth > 0);
1295 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1296 if (ndepth == 1) {
1297 /*
1298 * Direct child node of the images parent node,
1299 * i.e. component image node.
1300 */
1301 printf ("%s Image %u (%s)\n", p, count++,
1302 fit_get_name(fit, noffset, NULL));
1303
1304 fit_image_print (fit, noffset, p);
1305 }
1306 }
1307
1308 /* Find configurations parent node offset */
1309 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
1310 if (confs_noffset < 0) {
1311 debug ("Can't get configurations parent node '%s' (%s)\n",
1312 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
1313 return;
1314 }
1315
1316 /* get default configuration unit name from default property */
1317 uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
1318 if (uname)
1319 printf ("%s Default Configuration: '%s'\n", p, uname);
1320
1321 /* Process its subnodes, print out configurations details */
1322 for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
1323 (noffset >= 0) && (ndepth > 0);
1324 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1325 if (ndepth == 1) {
1326 /*
1327 * Direct child node of the configurations parent node,
1328 * i.e. configuration node.
1329 */
1330 printf ("%s Configuration %u (%s)\n", p, count++,
1331 fit_get_name(fit, noffset, NULL));
1332
1333 fit_conf_print (fit, noffset, p);
1334 }
1335 }
1336}
1337
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001338/**
1339 * fit_image_print - prints out the FIT component image details
1340 * @fit: pointer to the FIT format image header
1341 * @image_noffset: offset of the component image node
1342 * @p: pointer to prefix string
1343 *
1344 * fit_image_print() lists all mandatory properies for the processed component
1345 * image. If present, hash nodes are printed out as well.
1346 *
1347 * returns:
1348 * no returned results
1349 */
1350void fit_image_print (const void *fit, int image_noffset, const char *p)
1351{
1352 char *desc;
1353 uint8_t type, arch, os, comp;
1354 size_t size;
1355 ulong load, entry;
1356 const void *data;
1357 int noffset;
1358 int ndepth;
1359 int ret;
1360
1361 /* Mandatory properties */
1362 ret = fit_get_desc (fit, image_noffset, &desc);
1363 printf ("%s Description: ", p);
1364 if (ret)
1365 printf ("unavailable\n");
1366 else
1367 printf ("%s\n", desc);
1368
1369 fit_image_get_type (fit, image_noffset, &type);
1370 printf ("%s Type: %s\n", p, genimg_get_type_name (type));
1371
1372 fit_image_get_comp (fit, image_noffset, &comp);
1373 printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
1374
1375 ret = fit_image_get_data (fit, image_noffset, &data, &size);
1376
1377#ifndef USE_HOSTCC
1378 printf ("%s Data Start: ", p);
1379 if (ret)
1380 printf ("unavailable\n");
1381 else
1382 printf ("0x%08lx\n", (ulong)data);
1383#endif
1384
1385 printf ("%s Data Size: ", p);
1386 if (ret)
1387 printf ("unavailable\n");
1388 else
1389 genimg_print_size (size);
1390
1391 /* Remaining, type dependent properties */
1392 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
1393 (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
1394 (type == IH_TYPE_FLATDT)) {
1395 fit_image_get_arch (fit, image_noffset, &arch);
1396 printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
1397 }
1398
1399 if (type == IH_TYPE_KERNEL) {
1400 fit_image_get_os (fit, image_noffset, &os);
1401 printf ("%s OS: %s\n", p, genimg_get_os_name (os));
1402 }
1403
1404 if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
1405 ret = fit_image_get_load (fit, image_noffset, &load);
1406 printf ("%s Load Address: ", p);
1407 if (ret)
1408 printf ("unavailable\n");
1409 else
1410 printf ("0x%08lx\n", load);
1411
1412 fit_image_get_entry (fit, image_noffset, &entry);
1413 printf ("%s Entry Point: ", p);
1414 if (ret)
1415 printf ("unavailable\n");
1416 else
1417 printf ("0x%08lx\n", entry);
1418 }
1419
1420 /* Process all hash subnodes of the component image node */
1421 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
1422 (noffset >= 0) && (ndepth > 0);
1423 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1424 if (ndepth == 1) {
1425 /* Direct child node of the component image node */
1426 fit_image_print_hash (fit, noffset, p);
1427 }
1428 }
1429}
1430
1431/**
1432 * fit_image_print_hash - prints out the hash node details
1433 * @fit: pointer to the FIT format image header
1434 * @noffset: offset of the hash node
1435 * @p: pointer to prefix string
1436 *
1437 * fit_image_print_hash() lists properies for the processed hash node
1438 *
1439 * returns:
1440 * no returned results
1441 */
1442void fit_image_print_hash (const void *fit, int noffset, const char *p)
1443{
1444 char *algo;
1445 uint8_t *value;
1446 int value_len;
1447 int i, ret;
1448
1449 /*
1450 * Check subnode name, must be equal to "hash".
1451 * Multiple hash nodes require unique unit node
1452 * names, e.g. hash@1, hash@2, etc.
1453 */
1454 if (strncmp (fit_get_name(fit, noffset, NULL),
1455 FIT_HASH_NODENAME,
1456 strlen(FIT_HASH_NODENAME)) != 0)
1457 return;
1458
1459 debug ("%s Hash node: '%s'\n", p,
1460 fit_get_name (fit, noffset, NULL));
1461
1462 printf ("%s Hash algo: ", p);
1463 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
1464 printf ("invalid/unsupported\n");
1465 return;
1466 }
1467 printf ("%s\n", algo);
1468
1469 ret = fit_image_hash_get_value (fit, noffset, &value,
1470 &value_len);
1471 printf ("%s Hash value: ", p);
1472 if (ret) {
1473 printf ("unavailable\n");
1474 } else {
1475 for (i = 0; i < value_len; i++)
1476 printf ("%02x", value[i]);
1477 printf ("\n");
1478 }
1479
1480 debug ("%s Hash len: %d\n", p, value_len);
1481}
1482
1483/**
1484 * fit_get_desc - get node description property
1485 * @fit: pointer to the FIT format image header
1486 * @noffset: node offset
1487 * @desc: double pointer to the char, will hold pointer to the descrption
1488 *
1489 * fit_get_desc() reads description property from a given node, if
1490 * description is found pointer to it is returened in third call argument.
1491 *
1492 * returns:
1493 * 0, on success
1494 * -1, on failure
1495 */
1496int fit_get_desc (const void *fit, int noffset, char **desc)
1497{
1498 int len;
1499
1500 *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
1501 if (*desc == NULL) {
1502 fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
1503 return -1;
1504 }
1505
1506 return 0;
1507}
1508
1509/**
1510 * fit_get_timestamp - get node timestamp property
1511 * @fit: pointer to the FIT format image header
1512 * @noffset: node offset
1513 * @timestamp: pointer to the time_t, will hold read timestamp
1514 *
1515 * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
1516 * is found and has a correct size its value is retured in third call
1517 * argument.
1518 *
1519 * returns:
1520 * 0, on success
1521 * -1, on property read failure
1522 * -2, on wrong timestamp size
1523 */
1524int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
1525{
1526 int len;
1527 const void *data;
1528
1529 data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
1530 if (data == NULL) {
1531 fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
1532 return -1;
1533 }
1534 if (len != sizeof (uint32_t)) {
1535 debug ("FIT timestamp with incorrect size of (%u)\n", len);
1536 return -2;
1537 }
1538
1539 *timestamp = uimage_to_cpu (*((uint32_t *)data));
1540 return 0;
1541}
1542
1543/**
1544 * fit_image_get_node - get node offset for component image of a given unit name
1545 * @fit: pointer to the FIT format image header
1546 * @image_uname: component image node unit name
1547 *
1548 * fit_image_get_node() finds a component image (withing the '/images'
1549 * node) of a provided unit name. If image is found its node offset is
1550 * returned to the caller.
1551 *
1552 * returns:
1553 * image node offset when found (>=0)
1554 * negative number on failure (FDT_ERR_* code)
1555 */
1556int fit_image_get_node (const void *fit, const char *image_uname)
1557{
1558 int noffset, images_noffset;
1559
1560 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1561 if (images_noffset < 0) {
1562 debug ("Can't find images parent node '%s' (%s)\n",
1563 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1564 return images_noffset;
1565 }
1566
1567 noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
1568 if (noffset < 0) {
1569 debug ("Can't get node offset for image unit name: '%s' (%s)\n",
1570 image_uname, fdt_strerror (noffset));
1571 }
1572
1573 return noffset;
1574}
1575
1576/**
1577 * fit_image_get_os - get os id for a given component image node
1578 * @fit: pointer to the FIT format image header
1579 * @noffset: component image node offset
1580 * @os: pointer to the uint8_t, will hold os numeric id
1581 *
1582 * fit_image_get_os() finds os property in a given component image node.
1583 * If the property is found, its (string) value is translated to the numeric
1584 * id which is returned to the caller.
1585 *
1586 * returns:
1587 * 0, on success
1588 * -1, on failure
1589 */
1590int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
1591{
1592 int len;
1593 const void *data;
1594
1595 /* Get OS name from property data */
1596 data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
1597 if (data == NULL) {
1598 fit_get_debug (fit, noffset, FIT_OS_PROP, len);
1599 *os = -1;
1600 return -1;
1601 }
1602
1603 /* Translate OS name to id */
1604 *os = genimg_get_os_id (data);
1605 return 0;
1606}
1607
1608/**
1609 * fit_image_get_arch - get arch id for a given component image node
1610 * @fit: pointer to the FIT format image header
1611 * @noffset: component image node offset
1612 * @arch: pointer to the uint8_t, will hold arch numeric id
1613 *
1614 * fit_image_get_arch() finds arch property in a given component image node.
1615 * If the property is found, its (string) value is translated to the numeric
1616 * id which is returned to the caller.
1617 *
1618 * returns:
1619 * 0, on success
1620 * -1, on failure
1621 */
1622int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
1623{
1624 int len;
1625 const void *data;
1626
1627 /* Get architecture name from property data */
1628 data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
1629 if (data == NULL) {
1630 fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
1631 *arch = -1;
1632 return -1;
1633 }
1634
1635 /* Translate architecture name to id */
1636 *arch = genimg_get_arch_id (data);
1637 return 0;
1638}
1639
1640/**
1641 * fit_image_get_type - get type id for a given component image node
1642 * @fit: pointer to the FIT format image header
1643 * @noffset: component image node offset
1644 * @type: pointer to the uint8_t, will hold type numeric id
1645 *
1646 * fit_image_get_type() finds type property in a given component image node.
1647 * If the property is found, its (string) value is translated to the numeric
1648 * id which is returned to the caller.
1649 *
1650 * returns:
1651 * 0, on success
1652 * -1, on failure
1653 */
1654int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
1655{
1656 int len;
1657 const void *data;
1658
1659 /* Get image type name from property data */
1660 data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
1661 if (data == NULL) {
1662 fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
1663 *type = -1;
1664 return -1;
1665 }
1666
1667 /* Translate image type name to id */
1668 *type = genimg_get_type_id (data);
1669 return 0;
1670}
1671
1672/**
1673 * fit_image_get_comp - get comp id for a given component image node
1674 * @fit: pointer to the FIT format image header
1675 * @noffset: component image node offset
1676 * @comp: pointer to the uint8_t, will hold comp numeric id
1677 *
1678 * fit_image_get_comp() finds comp property in a given component image node.
1679 * If the property is found, its (string) value is translated to the numeric
1680 * id which is returned to the caller.
1681 *
1682 * returns:
1683 * 0, on success
1684 * -1, on failure
1685 */
1686int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
1687{
1688 int len;
1689 const void *data;
1690
1691 /* Get compression name from property data */
1692 data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
1693 if (data == NULL) {
1694 fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
1695 *comp = -1;
1696 return -1;
1697 }
1698
1699 /* Translate compression name to id */
1700 *comp = genimg_get_comp_id (data);
1701 return 0;
1702}
1703
1704/**
1705 * fit_image_get_load - get load address property for a given component image node
1706 * @fit: pointer to the FIT format image header
1707 * @noffset: component image node offset
1708 * @load: pointer to the uint32_t, will hold load address
1709 *
1710 * fit_image_get_load() finds load address property in a given component image node.
1711 * If the property is found, its value is returned to the caller.
1712 *
1713 * returns:
1714 * 0, on success
1715 * -1, on failure
1716 */
1717int fit_image_get_load (const void *fit, int noffset, ulong *load)
1718{
1719 int len;
1720 const uint32_t *data;
1721
1722 data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
1723 if (data == NULL) {
1724 fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
1725 return -1;
1726 }
1727
1728 *load = uimage_to_cpu (*data);
1729 return 0;
1730}
1731
1732/**
1733 * fit_image_get_entry - get entry point address property for a given component image node
1734 * @fit: pointer to the FIT format image header
1735 * @noffset: component image node offset
1736 * @entry: pointer to the uint32_t, will hold entry point address
1737 *
1738 * fit_image_get_entry() finds entry point address property in a given component image node.
1739 * If the property is found, its value is returned to the caller.
1740 *
1741 * returns:
1742 * 0, on success
1743 * -1, on failure
1744 */
1745int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
1746{
1747 int len;
1748 const uint32_t *data;
1749
1750 data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
1751 if (data == NULL) {
1752 fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
1753 return -1;
1754 }
1755
1756 *entry = uimage_to_cpu (*data);
1757 return 0;
1758}
1759
1760/**
1761 * fit_image_get_data - get data property and its size for a given component image node
1762 * @fit: pointer to the FIT format image header
1763 * @noffset: component image node offset
1764 * @data: double pointer to void, will hold data property's data address
1765 * @size: pointer to size_t, will hold data property's data size
1766 *
1767 * fit_image_get_data() finds data property in a given component image node.
1768 * If the property is found its data start address and size are returned to
1769 * the caller.
1770 *
1771 * returns:
1772 * 0, on success
1773 * -1, on failure
1774 */
1775int fit_image_get_data (const void *fit, int noffset,
1776 const void **data, size_t *size)
1777{
1778 int len;
1779
1780 *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
1781 if (*data == NULL) {
1782 fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
1783 *size = 0;
1784 return -1;
1785 }
1786
1787 *size = len;
1788 return 0;
1789}
1790
1791/**
1792 * fit_image_hash_get_algo - get hash algorithm name
1793 * @fit: pointer to the FIT format image header
1794 * @noffset: hash node offset
1795 * @algo: double pointer to char, will hold pointer to the algorithm name
1796 *
1797 * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
1798 * If the property is found its data start address is returned to the caller.
1799 *
1800 * returns:
1801 * 0, on success
1802 * -1, on failure
1803 */
1804int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
1805{
1806 int len;
1807
1808 *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
1809 if (*algo == NULL) {
1810 fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
1811 return -1;
1812 }
1813
1814 return 0;
1815}
1816
1817/**
1818 * fit_image_hash_get_value - get hash value and length
1819 * @fit: pointer to the FIT format image header
1820 * @noffset: hash node offset
1821 * @value: double pointer to uint8_t, will hold address of a hash value data
1822 * @value_len: pointer to an int, will hold hash data length
1823 *
1824 * fit_image_hash_get_value() finds hash value property in a given hash node.
1825 * If the property is found its data start address and size are returned to
1826 * the caller.
1827 *
1828 * returns:
1829 * 0, on success
1830 * -1, on failure
1831 */
1832int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
1833 int *value_len)
1834{
1835 int len;
1836
1837 *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
1838 if (*value == NULL) {
1839 fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
1840 *value_len = 0;
1841 return -1;
1842 }
1843
1844 *value_len = len;
1845 return 0;
1846}
1847
1848/**
1849 * fit_set_timestamp - set node timestamp property
1850 * @fit: pointer to the FIT format image header
1851 * @noffset: node offset
1852 * @timestamp: timestamp value to be set
1853 *
1854 * fit_set_timestamp() attempts to set timestamp property in the requested
1855 * node and returns operation status to the caller.
1856 *
1857 * returns:
1858 * 0, on success
1859 * -1, on property read failure
1860 */
1861int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
1862{
1863 uint32_t t;
1864 int ret;
1865
1866 t = cpu_to_uimage (timestamp);
1867 ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
1868 sizeof (uint32_t));
1869 if (ret) {
1870 printf ("Can't set '%s' property for '%s' node (%s)\n",
1871 FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
1872 fdt_strerror (ret));
1873 return -1;
1874 }
1875
1876 return 0;
1877}
1878
1879/**
1880 * calculate_hash - calculate and return hash for provided input data
1881 * @data: pointer to the input data
1882 * @data_len: data length
1883 * @algo: requested hash algorithm
1884 * @value: pointer to the char, will hold hash value data (caller must
1885 * allocate enough free space)
1886 * value_len: length of the calculated hash
1887 *
1888 * calculate_hash() computes input data hash according to the requested algorithm.
1889 * Resulting hash value is placed in caller provided 'value' buffer, length
1890 * of the calculated hash is returned via value_len pointer argument.
1891 *
1892 * returns:
1893 * 0, on success
1894 * -1, when algo is unsupported
1895 */
1896static int calculate_hash (const void *data, int data_len, const char *algo,
1897 uint8_t *value, int *value_len)
1898{
1899 if (strcmp (algo, "crc32") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02001900 *((uint32_t *)value) = crc32_wd (0, data, data_len,
1901 CHUNKSZ_CRC32);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001902 *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
1903 *value_len = 4;
1904 } else if (strcmp (algo, "sha1") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02001905 sha1_csum_wd ((unsigned char *) data, data_len,
1906 (unsigned char *) value, CHUNKSZ_SHA1);
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001907 *value_len = 20;
1908 } else if (strcmp (algo, "md5") == 0 ) {
Bartlomiej Sieka75903782008-04-25 13:54:02 +02001909 md5_wd ((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
Bartlomiej Sieka766529f2008-03-14 16:22:34 +01001910 *value_len = 16;
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001911 } else {
1912 debug ("Unsupported hash alogrithm\n");
1913 return -1;
1914 }
1915 return 0;
1916}
1917
1918#ifdef USE_HOSTCC
1919/**
1920 * fit_set_hashes - process FIT component image nodes and calculate hashes
1921 * @fit: pointer to the FIT format image header
1922 *
1923 * fit_set_hashes() adds hash values for all component images in the FIT blob.
1924 * Hashes are calculated for all component images which have hash subnodes
1925 * with algorithm property set to one of the supported hash algorithms.
1926 *
1927 * returns
1928 * 0, on success
1929 * libfdt error code, on failure
1930 */
1931int fit_set_hashes (void *fit)
1932{
1933 int images_noffset;
1934 int noffset;
1935 int ndepth;
1936 int ret;
1937
1938 /* Find images parent node offset */
1939 images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
1940 if (images_noffset < 0) {
1941 printf ("Can't find images parent node '%s' (%s)\n",
1942 FIT_IMAGES_PATH, fdt_strerror (images_noffset));
1943 return images_noffset;
1944 }
1945
1946 /* Process its subnodes, print out component images details */
1947 for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
1948 (noffset >= 0) && (ndepth > 0);
1949 noffset = fdt_next_node (fit, noffset, &ndepth)) {
1950 if (ndepth == 1) {
1951 /*
1952 * Direct child node of the images parent node,
1953 * i.e. component image node.
1954 */
1955 ret = fit_image_set_hashes (fit, noffset);
1956 if (ret)
1957 return ret;
1958 }
1959 }
1960
1961 return 0;
1962}
1963
1964/**
1965 * fit_image_set_hashes - calculate/set hashes for given component image node
1966 * @fit: pointer to the FIT format image header
1967 * @image_noffset: requested component image node
1968 *
1969 * fit_image_set_hashes() adds hash values for an component image node. All
1970 * existing hash subnodes are checked, if algorithm property is set to one of
1971 * the supported hash algorithms, hash value is computed and corresponding
1972 * hash node property is set, for example:
1973 *
1974 * Input component image node structure:
1975 *
1976 * o image@1 (at image_noffset)
1977 * | - data = [binary data]
1978 * o hash@1
1979 * |- algo = "sha1"
1980 *
1981 * Output component image node structure:
1982 *
1983 * o image@1 (at image_noffset)
1984 * | - data = [binary data]
1985 * o hash@1
1986 * |- algo = "sha1"
1987 * |- value = sha1(data)
1988 *
1989 * returns:
1990 * 0 on sucess
1991 * <0 on failure
1992 */
1993int fit_image_set_hashes (void *fit, int image_noffset)
1994{
1995 const void *data;
1996 size_t size;
1997 char *algo;
1998 uint8_t value[FIT_MAX_HASH_LEN];
1999 int value_len;
2000 int noffset;
2001 int ndepth;
2002
2003 /* Get image data and data length */
2004 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2005 printf ("Can't get image data/size\n");
2006 return -1;
2007 }
2008
2009 /* Process all hash subnodes of the component image node */
2010 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2011 (noffset >= 0) && (ndepth > 0);
2012 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2013 if (ndepth == 1) {
2014 /* Direct child node of the component image node */
2015
2016 /*
2017 * Check subnode name, must be equal to "hash".
2018 * Multiple hash nodes require unique unit node
2019 * names, e.g. hash@1, hash@2, etc.
2020 */
2021 if (strncmp (fit_get_name(fit, noffset, NULL),
2022 FIT_HASH_NODENAME,
2023 strlen(FIT_HASH_NODENAME)) != 0) {
2024 /* Not a hash subnode, skip it */
2025 continue;
2026 }
2027
2028 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2029 printf ("Can't get hash algo property for "
2030 "'%s' hash node in '%s' image node\n",
2031 fit_get_name (fit, noffset, NULL),
2032 fit_get_name (fit, image_noffset, NULL));
2033 return -1;
2034 }
2035
2036 if (calculate_hash (data, size, algo, value, &value_len)) {
2037 printf ("Unsupported hash algorithm (%s) for "
2038 "'%s' hash node in '%s' image node\n",
2039 algo, fit_get_name (fit, noffset, NULL),
2040 fit_get_name (fit, image_noffset, NULL));
2041 return -1;
2042 }
2043
2044 if (fit_image_hash_set_value (fit, noffset, value,
2045 value_len)) {
2046 printf ("Can't set hash value for "
2047 "'%s' hash node in '%s' image node\n",
2048 fit_get_name (fit, noffset, NULL),
2049 fit_get_name (fit, image_noffset, NULL));
2050 return -1;
2051 }
2052 }
2053 }
2054
2055 return 0;
2056}
2057
2058/**
2059 * fit_image_hash_set_value - set hash value in requested has node
2060 * @fit: pointer to the FIT format image header
2061 * @noffset: hash node offset
2062 * @value: hash value to be set
2063 * @value_len: hash value length
2064 *
2065 * fit_image_hash_set_value() attempts to set hash value in a node at offset
2066 * given and returns operation status to the caller.
2067 *
2068 * returns
2069 * 0, on success
2070 * -1, on failure
2071 */
2072int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
2073 int value_len)
2074{
2075 int ret;
2076
2077 ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
2078 if (ret) {
2079 printf ("Can't set hash '%s' property for '%s' node (%s)\n",
2080 FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
2081 fdt_strerror (ret));
2082 return -1;
2083 }
2084
2085 return 0;
2086}
2087#endif /* USE_HOSTCC */
2088
2089/**
2090 * fit_image_check_hashes - verify data intergity
2091 * @fit: pointer to the FIT format image header
2092 * @image_noffset: component image node offset
2093 *
2094 * fit_image_check_hashes() goes over component image hash nodes,
2095 * re-calculates each data hash and compares with the value stored in hash
2096 * node.
2097 *
2098 * returns:
2099 * 1, if all hashes are valid
2100 * 0, otherwise (or on error)
2101 */
2102int fit_image_check_hashes (const void *fit, int image_noffset)
2103{
2104 const void *data;
2105 size_t size;
2106 char *algo;
2107 uint8_t *fit_value;
2108 int fit_value_len;
2109 uint8_t value[FIT_MAX_HASH_LEN];
2110 int value_len;
2111 int noffset;
2112 int ndepth;
2113 char *err_msg = "";
2114
2115 /* Get image data and data length */
2116 if (fit_image_get_data (fit, image_noffset, &data, &size)) {
2117 printf ("Can't get image data/size\n");
2118 return 0;
2119 }
2120
2121 /* Process all hash subnodes of the component image node */
2122 for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
2123 (noffset >= 0) && (ndepth > 0);
2124 noffset = fdt_next_node (fit, noffset, &ndepth)) {
2125 if (ndepth == 1) {
2126 /* Direct child node of the component image node */
2127
2128 /*
2129 * Check subnode name, must be equal to "hash".
2130 * Multiple hash nodes require unique unit node
2131 * names, e.g. hash@1, hash@2, etc.
2132 */
2133 if (strncmp (fit_get_name(fit, noffset, NULL),
2134 FIT_HASH_NODENAME,
2135 strlen(FIT_HASH_NODENAME)) != 0)
2136 continue;
2137
2138 if (fit_image_hash_get_algo (fit, noffset, &algo)) {
2139 err_msg = "Can't get hash algo property";
2140 goto error;
2141 }
2142 printf ("%s", algo);
2143
2144 if (fit_image_hash_get_value (fit, noffset, &fit_value,
2145 &fit_value_len)) {
2146 err_msg = "Can't get hash value property";
2147 goto error;
2148 }
2149
2150 if (calculate_hash (data, size, algo, value, &value_len)) {
2151 err_msg = "Unsupported hash algorithm";
2152 goto error;
2153 }
2154
2155 if (value_len != fit_value_len) {
2156 err_msg = "Bad hash value len";
2157 goto error;
2158 } else if (memcmp (value, fit_value, value_len) != 0) {
2159 err_msg = "Bad hash value";
2160 goto error;
2161 }
2162 printf ("+ ");
2163 }
2164 }
2165
2166 return 1;
2167
2168error:
2169 printf ("%s for '%s' hash node in '%s' image node\n",
2170 err_msg, fit_get_name (fit, noffset, NULL),
2171 fit_get_name (fit, image_noffset, NULL));
2172 return 0;
2173}
2174
2175/**
2176 * fit_image_check_os - check whether image node is of a given os type
2177 * @fit: pointer to the FIT format image header
2178 * @noffset: component image node offset
2179 * @os: requested image os
2180 *
2181 * fit_image_check_os() reads image os property and compares its numeric
2182 * id with the requested os. Comparison result is returned to the caller.
2183 *
2184 * returns:
2185 * 1 if image is of given os type
2186 * 0 otherwise (or on error)
2187 */
2188int fit_image_check_os (const void *fit, int noffset, uint8_t os)
2189{
2190 uint8_t image_os;
2191
2192 if (fit_image_get_os (fit, noffset, &image_os))
2193 return 0;
2194 return (os == image_os);
2195}
2196
2197/**
2198 * fit_image_check_arch - check whether image node is of a given arch
2199 * @fit: pointer to the FIT format image header
2200 * @noffset: component image node offset
2201 * @arch: requested imagearch
2202 *
2203 * fit_image_check_arch() reads image arch property and compares its numeric
2204 * id with the requested arch. Comparison result is returned to the caller.
2205 *
2206 * returns:
2207 * 1 if image is of given arch
2208 * 0 otherwise (or on error)
2209 */
2210int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
2211{
2212 uint8_t image_arch;
2213
2214 if (fit_image_get_arch (fit, noffset, &image_arch))
2215 return 0;
2216 return (arch == image_arch);
2217}
2218
2219/**
2220 * fit_image_check_type - check whether image node is of a given type
2221 * @fit: pointer to the FIT format image header
2222 * @noffset: component image node offset
2223 * @type: requested image type
2224 *
2225 * fit_image_check_type() reads image type property and compares its numeric
2226 * id with the requested type. Comparison result is returned to the caller.
2227 *
2228 * returns:
2229 * 1 if image is of given type
2230 * 0 otherwise (or on error)
2231 */
2232int fit_image_check_type (const void *fit, int noffset, uint8_t type)
2233{
2234 uint8_t image_type;
2235
2236 if (fit_image_get_type (fit, noffset, &image_type))
2237 return 0;
2238 return (type == image_type);
2239}
2240
2241/**
2242 * fit_image_check_comp - check whether image node uses given compression
2243 * @fit: pointer to the FIT format image header
2244 * @noffset: component image node offset
2245 * @comp: requested image compression type
2246 *
2247 * fit_image_check_comp() reads image compression property and compares its
2248 * numeric id with the requested compression type. Comparison result is
2249 * returned to the caller.
2250 *
2251 * returns:
2252 * 1 if image uses requested compression
2253 * 0 otherwise (or on error)
2254 */
2255int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
2256{
2257 uint8_t image_comp;
2258
2259 if (fit_image_get_comp (fit, noffset, &image_comp))
2260 return 0;
2261 return (comp == image_comp);
2262}
2263
2264/**
2265 * fit_check_format - sanity check FIT image format
2266 * @fit: pointer to the FIT format image header
2267 *
2268 * fit_check_format() runs a basic sanity FIT image verification.
2269 * Routine checks for mandatory properties, nodes, etc.
2270 *
2271 * returns:
2272 * 1, on success
2273 * 0, on failure
2274 */
2275int fit_check_format (const void *fit)
2276{
2277 /* mandatory / node 'description' property */
2278 if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
2279 debug ("Wrong FIT format: no description\n");
2280 return 0;
2281 }
2282
2283#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
2284 /* mandatory / node 'timestamp' property */
2285 if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
2286 debug ("Wrong FIT format: no description\n");
2287 return 0;
2288 }
2289#endif
2290
2291 /* mandatory subimages parent '/images' node */
2292 if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
2293 debug ("Wrong FIT format: no images parent node\n");
2294 return 0;
2295 }
2296
2297 return 1;
2298}
2299
2300/**
2301 * fit_conf_get_node - get node offset for configuration of a given unit name
2302 * @fit: pointer to the FIT format image header
2303 * @conf_uname: configuration node unit name
2304 *
2305 * fit_conf_get_node() finds a configuration (withing the '/configurations'
2306 * parant node) of a provided unit name. If configuration is found its node offset
2307 * is returned to the caller.
2308 *
2309 * When NULL is provided in second argument fit_conf_get_node() will search
2310 * for a default configuration node instead. Default configuration node unit name
2311 * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
2312 *
2313 * returns:
2314 * configuration node offset when found (>=0)
2315 * negative number on failure (FDT_ERR_* code)
2316 */
2317int fit_conf_get_node (const void *fit, const char *conf_uname)
2318{
2319 int noffset, confs_noffset;
2320 int len;
2321
2322 confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
2323 if (confs_noffset < 0) {
2324 debug ("Can't find configurations parent node '%s' (%s)\n",
2325 FIT_CONFS_PATH, fdt_strerror (confs_noffset));
2326 return confs_noffset;
2327 }
2328
2329 if (conf_uname == NULL) {
2330 /* get configuration unit name from the default property */
2331 debug ("No configuration specified, trying default...\n");
2332 conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
2333 if (conf_uname == NULL) {
2334 fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
2335 return len;
2336 }
2337 debug ("Found default configuration: '%s'\n", conf_uname);
2338 }
2339
2340 noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
2341 if (noffset < 0) {
2342 debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
2343 conf_uname, fdt_strerror (noffset));
2344 }
2345
2346 return noffset;
2347}
2348
2349static int __fit_conf_get_prop_node (const void *fit, int noffset,
2350 const char *prop_name)
2351{
2352 char *uname;
2353 int len;
2354
2355 /* get kernel image unit name from configuration kernel property */
2356 uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
2357 if (uname == NULL)
2358 return len;
2359
2360 return fit_image_get_node (fit, uname);
2361}
2362
2363/**
2364 * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
2365 * a given configuration
2366 * @fit: pointer to the FIT format image header
2367 * @noffset: configuration node offset
2368 *
2369 * fit_conf_get_kernel_node() retrives kernel image node unit name from
2370 * configuration FIT_KERNEL_PROP property and translates it to the node
2371 * offset.
2372 *
2373 * returns:
2374 * image node offset when found (>=0)
2375 * negative number on failure (FDT_ERR_* code)
2376 */
2377int fit_conf_get_kernel_node (const void *fit, int noffset)
2378{
2379 return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
2380}
2381
2382/**
2383 * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
2384 * a given configuration
2385 * @fit: pointer to the FIT format image header
2386 * @noffset: configuration node offset
2387 *
2388 * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
2389 * configuration FIT_KERNEL_PROP property and translates it to the node
2390 * offset.
2391 *
2392 * returns:
2393 * image node offset when found (>=0)
2394 * negative number on failure (FDT_ERR_* code)
2395 */
2396int fit_conf_get_ramdisk_node (const void *fit, int noffset)
2397{
2398 return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
2399}
2400
2401/**
2402 * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
2403 * a given configuration
2404 * @fit: pointer to the FIT format image header
2405 * @noffset: configuration node offset
2406 *
2407 * fit_conf_get_fdt_node() retrives fdt image node unit name from
2408 * configuration FIT_KERNEL_PROP property and translates it to the node
2409 * offset.
2410 *
2411 * returns:
2412 * image node offset when found (>=0)
2413 * negative number on failure (FDT_ERR_* code)
2414 */
2415int fit_conf_get_fdt_node (const void *fit, int noffset)
2416{
2417 return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
2418}
2419
2420/**
2421 * fit_conf_print - prints out the FIT configuration details
2422 * @fit: pointer to the FIT format image header
Marian Balakowiczf773bea2008-03-12 10:35:46 +01002423 * @noffset: offset of the configuration node
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002424 * @p: pointer to prefix string
2425 *
2426 * fit_conf_print() lists all mandatory properies for the processed
2427 * configuration node.
2428 *
2429 * returns:
2430 * no returned results
2431 */
2432void fit_conf_print (const void *fit, int noffset, const char *p)
2433{
2434 char *desc;
2435 char *uname;
2436 int ret;
2437
2438 /* Mandatory properties */
2439 ret = fit_get_desc (fit, noffset, &desc);
2440 printf ("%s Description: ", p);
2441 if (ret)
2442 printf ("unavailable\n");
2443 else
2444 printf ("%s\n", desc);
2445
2446 uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
2447 printf ("%s Kernel: ", p);
2448 if (uname == NULL)
2449 printf ("unavailable\n");
2450 else
2451 printf ("%s\n", uname);
2452
2453 /* Optional properties */
2454 uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
2455 if (uname)
2456 printf ("%s Init Ramdisk: %s\n", p, uname);
2457
2458 uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
2459 if (uname)
2460 printf ("%s FDT: %s\n", p, uname);
2461}
Marian Balakowiczc8779642008-03-12 10:12:37 +01002462
2463/**
2464 * fit_check_ramdisk - verify FIT format ramdisk subimage
2465 * @fit_hdr: pointer to the FIT ramdisk header
2466 * @rd_noffset: ramdisk subimage node offset within FIT image
2467 * @arch: requested ramdisk image architecture type
2468 * @verify: data CRC verification flag
2469 *
2470 * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
2471 * specified FIT image.
2472 *
2473 * returns:
2474 * 1, on success
2475 * 0, on failure
2476 */
2477#ifndef USE_HOSTCC
2478static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
2479{
2480 fit_image_print (fit, rd_noffset, " ");
2481
2482 if (verify) {
2483 puts (" Verifying Hash Integrity ... ");
2484 if (!fit_image_check_hashes (fit, rd_noffset)) {
2485 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +01002486 show_boot_progress (-125);
Marian Balakowiczc8779642008-03-12 10:12:37 +01002487 return 0;
2488 }
2489 puts ("OK\n");
2490 }
2491
Marian Balakowicz1372cce2008-03-12 10:33:01 +01002492 show_boot_progress (126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01002493 if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
2494 !fit_image_check_arch (fit, rd_noffset, arch) ||
2495 !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
2496 printf ("No Linux %s Ramdisk Image\n",
2497 genimg_get_arch_name(arch));
Marian Balakowicz1372cce2008-03-12 10:33:01 +01002498 show_boot_progress (-126);
Marian Balakowiczc8779642008-03-12 10:12:37 +01002499 return 0;
2500 }
2501
Marian Balakowicz1372cce2008-03-12 10:33:01 +01002502 show_boot_progress (127);
Marian Balakowiczc8779642008-03-12 10:12:37 +01002503 return 1;
2504}
2505#endif /* USE_HOSTCC */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01002506#endif /* CONFIG_FIT */