blob: 7412a0ef48938dc2d32d7c95447395bdbe50341e [file] [log] [blame]
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
Marian Balakowiczceaed2b2008-01-31 13:57:17 +010025
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010026#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010027#include <common.h>
28#include <watchdog.h>
29
30#ifdef CONFIG_SHOW_BOOT_PROGRESS
31#include <status_led.h>
32#endif
33
34#ifdef CONFIG_HAS_DATAFLASH
35#include <dataflash.h>
36#endif
37
Marian Balakowicz95d449a2008-05-13 15:53:29 +020038#ifdef CONFIG_LOGBUFFER
39#include <logbuff.h>
40#endif
41
Marian Balakowicz2242f532008-02-21 17:27:41 +010042#include <rtc.h>
Marian Balakowicz2242f532008-02-21 17:27:41 +010043
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -060044#include <environment.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010045#include <image.h>
46
Stephen Warren712fbcf2011-10-18 11:11:49 +000047#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +010048#include <libfdt.h>
49#include <fdt_support.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010050#endif
51
Andy Fleming20a14a42008-04-02 16:19:07 -050052#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010053#include <sha1.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010054
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010055#ifdef CONFIG_CMD_BDI
Wolfgang Denk54841ab2010-06-28 22:00:46 +020056extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010057#endif
58
59DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010060
Stephen Warren712fbcf2011-10-18 11:11:49 +000061static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010062 int verify);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010063#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010064#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050065#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010066#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010067#include <image.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010068#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010069
Simon Glass0ccff502013-02-24 17:33:25 +000070#include <u-boot/crc.h>
71
Mike Frysinger7edb1862010-10-20 07:17:39 -040072static const table_entry_t uimage_arch[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +010073 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
74 { IH_ARCH_ALPHA, "alpha", "Alpha", },
75 { IH_ARCH_ARM, "arm", "ARM", },
76 { IH_ARCH_I386, "x86", "Intel x86", },
77 { IH_ARCH_IA64, "ia64", "IA64", },
78 { IH_ARCH_M68K, "m68k", "M68K", },
79 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
80 { IH_ARCH_MIPS, "mips", "MIPS", },
81 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010082 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070083 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010084 { IH_ARCH_PPC, "ppc", "PowerPC", },
85 { IH_ARCH_S390, "s390", "IBM S390", },
86 { IH_ARCH_SH, "sh", "SuperH", },
87 { IH_ARCH_SPARC, "sparc", "SPARC", },
88 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
89 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
90 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin64d61462011-10-19 20:41:09 +000091 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson3ddcacc2011-11-26 19:04:50 +000092 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Marian Balakowicz570abb02008-02-29 15:59:59 +010093 { -1, "", "", },
94};
95
Mike Frysinger7edb1862010-10-20 07:17:39 -040096static const table_entry_t uimage_os[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +010097 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010098 { IH_OS_LINUX, "linux", "Linux", },
99#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
100 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
101#endif
102 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +0200103 { IH_OS_OSE, "ose", "Enea OSE", },
Steven Stallion04d41402013-03-20 06:31:35 +0000104 { IH_OS_PLAN9, "plan9", "Plan 9", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100105 { IH_OS_RTEMS, "rtems", "RTEMS", },
106 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
107#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
108 { IH_OS_QNX, "qnx", "QNX", },
109 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
110#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500111#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
112 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
113#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100114#ifdef USE_HOSTCC
115 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
116 { IH_OS_DELL, "dell", "Dell", },
117 { IH_OS_ESIX, "esix", "Esix", },
118 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
119 { IH_OS_IRIX, "irix", "Irix", },
120 { IH_OS_NCR, "ncr", "NCR", },
121 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
122 { IH_OS_PSOS, "psos", "pSOS", },
123 { IH_OS_SCO, "sco", "SCO", },
124 { IH_OS_SOLARIS, "solaris", "Solaris", },
125 { IH_OS_SVR4, "svr4", "SVR4", },
126#endif
127 { -1, "", "", },
128};
129
Mike Frysinger7edb1862010-10-20 07:17:39 -0400130static const table_entry_t uimage_type[] = {
Stefano Babic4962e382011-10-17 00:07:43 +0000131 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100132 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
133 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby3decb142011-07-21 09:10:30 -0400134 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100135 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e82011-11-10 13:17:53 -0700136 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000137 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
138 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
139 { IH_TYPE_INVALID, NULL, "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100140 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000141 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Shaohui Xie5d898a02012-08-10 02:49:35 +0000142 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100143 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
144 { IH_TYPE_SCRIPT, "script", "Script", },
145 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000146 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100147 { -1, "", "", },
148};
149
Mike Frysinger7edb1862010-10-20 07:17:39 -0400150static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100151 { IH_COMP_NONE, "none", "uncompressed", },
152 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
153 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200154 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100155 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100156 { -1, "", "", },
157};
158
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100159/*****************************************************************************/
160/* Legacy format routines */
161/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000162int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100163{
164 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000165 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100166 image_header_t header;
167
168 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000169 memmove(&header, (char *)hdr, image_get_header_size());
170 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100171
Stephen Warren712fbcf2011-10-18 11:11:49 +0000172 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100173
Stephen Warren712fbcf2011-10-18 11:11:49 +0000174 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100175}
176
Stephen Warren712fbcf2011-10-18 11:11:49 +0000177int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100178{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000179 ulong data = image_get_data(hdr);
180 ulong len = image_get_data_size(hdr);
181 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100182
Stephen Warren712fbcf2011-10-18 11:11:49 +0000183 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100184}
185
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100186/**
187 * image_multi_count - get component (sub-image) count
188 * @hdr: pointer to the header of the multi component image
189 *
190 * image_multi_count() returns number of components in a multi
191 * component image.
192 *
193 * Note: no checking of the image type is done, caller must pass
194 * a valid multi component image.
195 *
196 * returns:
197 * number of components
198 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000199ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100200{
201 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100202 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100203
204 /* get start of the image payload, which in case of multi
205 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000206 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100207
208 /* count non empty slots */
209 for (i = 0; size[i]; ++i)
210 count++;
211
212 return count;
213}
214
215/**
216 * image_multi_getimg - get component data address and size
217 * @hdr: pointer to the header of the multi component image
218 * @idx: index of the requested component
219 * @data: pointer to a ulong variable, will hold component data address
220 * @len: pointer to a ulong variable, will hold component size
221 *
222 * image_multi_getimg() returns size and data address for the requested
223 * component in a multi component image.
224 *
225 * Note: no checking of the image type is done, caller must pass
226 * a valid multi component image.
227 *
228 * returns:
229 * data address and size of the component, if idx is valid
230 * 0 in data and len, if idx is out of range
231 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000232void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100233 ulong *data, ulong *len)
234{
235 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100236 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700237 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100238
239 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000240 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100241
242 /* get start of the image payload, which in case of multi
243 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000244 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100245
246 /* get address of the proper component data start, which means
247 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000248 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100249
250 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000251 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100252 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100253
254 /* go over all indices preceding requested component idx */
255 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700256 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000257 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100258 }
259
260 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700261 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100262 } else {
263 *len = 0;
264 *data = 0;
265 }
266}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100267
Stephen Warren712fbcf2011-10-18 11:11:49 +0000268static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100269{
270 const char *os, *arch, *type, *comp;
271
Stephen Warren712fbcf2011-10-18 11:11:49 +0000272 os = genimg_get_os_name(image_get_os(hdr));
273 arch = genimg_get_arch_name(image_get_arch(hdr));
274 type = genimg_get_type_name(image_get_type(hdr));
275 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100276
Stephen Warren712fbcf2011-10-18 11:11:49 +0000277 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100278}
279
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100280/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200281 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200282 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100283 * @p: pointer to prefix string
284 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200285 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100286 * The routine prints out all header fields followed by the size/offset data
287 * for MULTI/SCRIPT images.
288 *
289 * returns:
290 * no returned results
291 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000292void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100293{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200294 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200295 const char *p;
296
297#ifdef USE_HOSTCC
298 p = "";
299#else
300 p = " ";
301#endif
302
Stephen Warren712fbcf2011-10-18 11:11:49 +0000303 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Simon Glass859e92b2013-05-07 06:11:51 +0000304 if (IMAGE_ENABLE_TIMESTAMP) {
305 printf("%sCreated: ", p);
306 genimg_print_time((time_t)image_get_time(hdr));
307 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000308 printf("%sImage Type: ", p);
309 image_print_type(hdr);
310 printf("%sData Size: ", p);
311 genimg_print_size(image_get_data_size(hdr));
312 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
313 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100314
Stephen Warren712fbcf2011-10-18 11:11:49 +0000315 if (image_check_type(hdr, IH_TYPE_MULTI) ||
316 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100317 int i;
318 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000319 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100320
Stephen Warren712fbcf2011-10-18 11:11:49 +0000321 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100322 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000323 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100324
Stephen Warren712fbcf2011-10-18 11:11:49 +0000325 printf("%s Image %d: ", p, i);
326 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100327
Stephen Warren712fbcf2011-10-18 11:11:49 +0000328 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100329 /*
330 * the user may need to know offsets
331 * if planning to do something with
332 * multiple files
333 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000334 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100335 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100336 }
337 }
338}
339
Marian Balakowicz570abb02008-02-29 15:59:59 +0100340
341#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100342/**
343 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100344 * @rd_addr: ramdisk image start address
345 * @arch: expected ramdisk architecture
346 * @verify: checksum verification flag
347 *
348 * image_get_ramdisk() returns a pointer to the verified ramdisk image
349 * header. Routine receives image start address and expected architecture
350 * flag. Verification done covers data and header integrity and os/type/arch
351 * fields checking.
352 *
353 * If dataflash support is enabled routine checks for dataflash addresses
354 * and handles required dataflash reads.
355 *
356 * returns:
357 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600358 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100359 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000360static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100361 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100362{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200363 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100364
Stephen Warren712fbcf2011-10-18 11:11:49 +0000365 if (!image_check_magic(rd_hdr)) {
366 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000367 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Kumar Gala274cea22008-02-27 21:51:46 -0600368 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100369 }
370
Stephen Warren712fbcf2011-10-18 11:11:49 +0000371 if (!image_check_hcrc(rd_hdr)) {
372 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000373 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600374 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100375 }
376
Simon Glass770605e2012-02-13 13:51:18 +0000377 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000378 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100379
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100380 if (verify) {
381 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000382 if (!image_check_dcrc(rd_hdr)) {
383 puts("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000384 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600385 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100386 }
387 puts("OK\n");
388 }
389
Simon Glass770605e2012-02-13 13:51:18 +0000390 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100391
Stephen Warren712fbcf2011-10-18 11:11:49 +0000392 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
393 !image_check_arch(rd_hdr, arch) ||
394 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
395 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100396 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +0000397 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Kumar Gala274cea22008-02-27 21:51:46 -0600398 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100399 }
400
401 return rd_hdr;
402}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100403#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100404
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100405/*****************************************************************************/
406/* Shared dual-format routines */
407/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100408#ifndef USE_HOSTCC
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600409ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
410ulong save_addr; /* Default Save Address */
411ulong save_size; /* Default Save Size (in bytes) */
412
413static int on_loadaddr(const char *name, const char *value, enum env_op op,
414 int flags)
415{
416 switch (op) {
417 case env_op_create:
418 case env_op_overwrite:
419 load_addr = simple_strtoul(value, NULL, 16);
420 break;
421 default:
422 break;
423 }
424
425 return 0;
426}
427U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
428
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100429ulong getenv_bootm_low(void)
430{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000431 char *s = getenv("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100432 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000433 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100434 return tmp;
435 }
436
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200437#if defined(CONFIG_SYS_SDRAM_BASE)
438 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100439#elif defined(CONFIG_ARM)
440 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100441#else
442 return 0;
443#endif
444}
445
Becky Bruce391fd932008-06-09 20:37:18 -0500446phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100447{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500448 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000449 char *s = getenv("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100450 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000451 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100452 return tmp;
453 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500454 s = getenv("bootm_low");
455 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000456 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500457 else
458 tmp = 0;
459
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100460
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100461#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500462 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100463#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500464 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100465#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100466}
467
Grant Likelyc3624e62011-03-28 09:58:43 +0000468phys_size_t getenv_bootm_mapsize(void)
469{
470 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000471 char *s = getenv("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000472 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000473 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000474 return tmp;
475 }
476
477#if defined(CONFIG_SYS_BOOTMAPSZ)
478 return CONFIG_SYS_BOOTMAPSZ;
479#else
480 return getenv_bootm_size();
481#endif
482}
483
Stephen Warren712fbcf2011-10-18 11:11:49 +0000484void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100485{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400486 if (to == from)
487 return;
488
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100489#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
490 while (len > 0) {
491 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000492 WATCHDOG_RESET();
493 memmove(to, from, tail);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100494 to += tail;
495 from += tail;
496 len -= tail;
497 }
498#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000499 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100500#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
501}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100502#endif /* !USE_HOSTCC */
503
Stephen Warren712fbcf2011-10-18 11:11:49 +0000504void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100505{
506#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000507 printf("%d Bytes = ", size);
508 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100509#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000510 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100511 size, (double)size / 1.024e3,
512 (double)size / 1.048576e6);
513#endif
514}
515
Simon Glass859e92b2013-05-07 06:11:51 +0000516#if IMAGE_ENABLE_TIMESTAMP
517void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100518{
519#ifndef USE_HOSTCC
520 struct rtc_time tm;
521
Stephen Warren712fbcf2011-10-18 11:11:49 +0000522 to_tm(timestamp, &tm);
523 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100524 tm.tm_year, tm.tm_mon, tm.tm_mday,
525 tm.tm_hour, tm.tm_min, tm.tm_sec);
526#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000527 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100528#endif
529}
Simon Glass859e92b2013-05-07 06:11:51 +0000530#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100531
532/**
533 * get_table_entry_name - translate entry id to long name
534 * @table: pointer to a translation table for entries of a specific type
535 * @msg: message to be returned when translation fails
536 * @id: entry id to be translated
537 *
538 * get_table_entry_name() will go over translation table trying to find
539 * entry that matches given id. If matching entry is found, its long
540 * name is returned to the caller.
541 *
542 * returns:
543 * long entry name if translation succeeds
544 * msg otherwise
545 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400546char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100547{
548 for (; table->id >= 0; ++table) {
549 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200550#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500551 return table->lname;
552#else
553 return table->lname + gd->reloc_off;
554#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100555 }
556 return (msg);
557}
558
Stephen Warren712fbcf2011-10-18 11:11:49 +0000559const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100560{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000561 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100562}
563
Stephen Warren712fbcf2011-10-18 11:11:49 +0000564const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100565{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000566 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
567 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100568}
569
Stephen Warren712fbcf2011-10-18 11:11:49 +0000570const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100571{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000572 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100573}
574
Stephen Warren712fbcf2011-10-18 11:11:49 +0000575const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100576{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000577 return (get_table_entry_name(uimage_comp, "Unknown Compression",
578 comp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100579}
580
581/**
582 * get_table_entry_id - translate short entry name to id
583 * @table: pointer to a translation table for entries of a specific type
584 * @table_name: to be used in case of error
585 * @name: entry short name to be translated
586 *
587 * get_table_entry_id() will go over translation table trying to find
588 * entry that matches given short name. If matching entry is found,
589 * its id returned to the caller.
590 *
591 * returns:
592 * entry id if translation succeeds
593 * -1 otherwise
594 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400595int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100596 const char *table_name, const char *name)
597{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400598 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100599#ifdef USE_HOSTCC
600 int first = 1;
601
602 for (t = table; t->id >= 0; ++t) {
603 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000604 return(t->id);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100605 }
606
Stephen Warren712fbcf2011-10-18 11:11:49 +0000607 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100608 for (t = table; t->id >= 0; ++t) {
609 if (t->sname == NULL)
610 continue;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000611 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100612 first = 0;
613 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000614 fprintf(stderr, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100615#else
616 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200617#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500618 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200619#else
620 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500621#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100622 return (t->id);
623 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000624 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100625#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100626 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100627}
628
Stephen Warren712fbcf2011-10-18 11:11:49 +0000629int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100630{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000631 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100632}
633
Stephen Warren712fbcf2011-10-18 11:11:49 +0000634int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100635{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000636 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100637}
638
Stephen Warren712fbcf2011-10-18 11:11:49 +0000639int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100640{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000641 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100642}
643
Stephen Warren712fbcf2011-10-18 11:11:49 +0000644int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100645{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000646 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100647}
648
649#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100650/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100651 * genimg_get_format - get image format type
652 * @img_addr: image start address
653 *
654 * genimg_get_format() checks whether provided address points to a valid
655 * legacy or FIT image.
656 *
657 * New uImage format and FDT blob are based on a libfdt. FDT blob
658 * may be passed directly or embedded in a FIT image. In both situations
659 * genimg_get_format() must be able to dectect libfdt header.
660 *
661 * returns:
662 * image format type or IMAGE_FORMAT_INVALID if no image is present
663 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000664int genimg_get_format(void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100665{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200666 ulong format = IMAGE_FORMAT_INVALID;
667 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100668#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200669 char *fit_hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100670#endif
671
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200672 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100673 if (image_check_magic(hdr))
674 format = IMAGE_FORMAT_LEGACY;
675#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
676 else {
677 fit_hdr = (char *)img_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000678 if (fdt_check_header(fit_hdr) == 0)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100679 format = IMAGE_FORMAT_FIT;
680 }
681#endif
682
683 return format;
684}
685
686/**
687 * genimg_get_image - get image from special storage (if necessary)
688 * @img_addr: image start address
689 *
690 * genimg_get_image() checks if provided image start adddress is located
691 * in a dataflash storage. If so, image is moved to a system RAM memory.
692 *
693 * returns:
694 * image start address after possible relocation from special storage
695 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000696ulong genimg_get_image(ulong img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100697{
698 ulong ram_addr = img_addr;
699
700#ifdef CONFIG_HAS_DATAFLASH
701 ulong h_size, d_size;
702
Stephen Warren712fbcf2011-10-18 11:11:49 +0000703 if (addr_dataflash(img_addr)) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100704 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200705 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100706
707 /* get header size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000708 h_size = image_get_header_size();
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100709#if defined(CONFIG_FIT)
710 if (sizeof(struct fdt_header) > h_size)
711 h_size = sizeof(struct fdt_header);
712#endif
713
714 /* read in header */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000715 debug(" Reading image header from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100716 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
717
Stephen Warren712fbcf2011-10-18 11:11:49 +0000718 read_dataflash(img_addr, h_size, (char *)ram_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100719
720 /* get data size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000721 switch (genimg_get_format((void *)ram_addr)) {
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100722 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000723 d_size = image_get_data_size(
724 (const image_header_t *)ram_addr);
725 debug(" Legacy format image found at 0x%08lx, "
726 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100727 ram_addr, d_size);
728 break;
729#if defined(CONFIG_FIT)
730 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000731 d_size = fit_get_size((const void *)ram_addr) - h_size;
732 debug(" FIT/FDT format image found at 0x%08lx, "
733 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100734 ram_addr, d_size);
735 break;
736#endif
737 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000738 printf(" No valid image found at 0x%08lx\n",
739 img_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100740 return ram_addr;
741 }
742
743 /* read in image data */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000744 debug(" Reading image remaining data from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100745 "%08lx to RAM address %08lx\n", img_addr + h_size,
746 ram_addr + h_size);
747
Stephen Warren712fbcf2011-10-18 11:11:49 +0000748 read_dataflash(img_addr + h_size, d_size,
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100749 (char *)(ram_addr + h_size));
750
751 }
752#endif /* CONFIG_HAS_DATAFLASH */
753
754 return ram_addr;
755}
756
757/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100758 * fit_has_config - check if there is a valid FIT configuration
759 * @images: pointer to the bootm command headers structure
760 *
761 * fit_has_config() checks if there is a FIT configuration in use
762 * (if FTI support is present).
763 *
764 * returns:
765 * 0, no FIT support or no configuration found
766 * 1, configuration found
767 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000768int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100769{
770#if defined(CONFIG_FIT)
771 if (images->fit_uname_cfg)
772 return 1;
773#endif
774 return 0;
775}
776
777/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100778 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100779 * @argc: command argument count
780 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100781 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100782 * @arch: expected ramdisk architecture
783 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
784 * @rd_end: pointer to a ulong variable, will hold ramdisk end
785 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100786 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100787 * Curently supported are the following ramdisk sources:
788 * - multicomponent kernel/ramdisk image,
789 * - commandline provided address of decicated ramdisk image.
790 *
791 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100792 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100793 * rd_start and rd_end are set to ramdisk start/end addresses if
794 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100795 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500796 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100797 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100798 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000799int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100800 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100801{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100802 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100803 ulong rd_data, rd_len;
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200804 const image_header_t *rd_hdr;
Marek Vasut57d40ab2012-03-30 23:19:10 +0200805#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut017e1f32012-03-18 11:47:58 +0000806 char *end;
Marek Vasut57d40ab2012-03-30 23:19:10 +0200807#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100808#if defined(CONFIG_FIT)
809 void *fit_hdr;
810 const char *fit_uname_config = NULL;
811 const char *fit_uname_ramdisk = NULL;
812 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100813 int rd_noffset;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100814 int cfg_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100815 const void *data;
816 size_t size;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100817#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100818
Marian Balakowiczc8779642008-03-12 10:12:37 +0100819 *rd_start = 0;
820 *rd_end = 0;
821
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100822 /*
823 * Look for a '-' which indicates to ignore the
824 * ramdisk argument
825 */
826 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000827 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100828 rd_len = rd_data = 0;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000829 } else if (argc >= 3 || genimg_has_config(images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100830#if defined(CONFIG_FIT)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100831 if (argc >= 3) {
832 /*
833 * If the init ramdisk comes from the FIT image and
834 * the FIT image address is omitted in the command
835 * line argument, try to use os FIT image address or
836 * default load address.
837 */
838 if (images->fit_uname_os)
839 default_addr = (ulong)images->fit_hdr_os;
840 else
841 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100842
Stephen Warren712fbcf2011-10-18 11:11:49 +0000843 if (fit_parse_conf(argv[2], default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100844 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000845 debug("* ramdisk: config '%s' from image at "
846 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100847 fit_uname_config, rd_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000848 } else if (fit_parse_subimage(argv[2], default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100849 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000850 debug("* ramdisk: subimage '%s' from image at "
851 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100852 fit_uname_ramdisk, rd_addr);
853 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100854#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100855 {
856 rd_addr = simple_strtoul(argv[2], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000857 debug("* ramdisk: cmdline image address = "
858 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100859 rd_addr);
860 }
861#if defined(CONFIG_FIT)
862 } else {
863 /* use FIT configuration provided in first bootm
864 * command argument
865 */
866 rd_addr = (ulong)images->fit_hdr_os;
867 fit_uname_config = images->fit_uname_cfg;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000868 debug("* ramdisk: using config '%s' from image "
869 "at 0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100870 fit_uname_config, rd_addr);
871
872 /*
873 * Check whether configuration has ramdisk defined,
874 * if not, don't try to use it, quit silently.
875 */
876 fit_hdr = (void *)rd_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000877 cfg_noffset = fit_conf_get_node(fit_hdr,
878 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100879 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000880 debug("* ramdisk: no such config\n");
Michal Simekc78fce62008-07-11 10:43:13 +0200881 return 1;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100882 }
883
Stephen Warren712fbcf2011-10-18 11:11:49 +0000884 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
885 cfg_noffset);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100886 if (rd_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000887 debug("* ramdisk: no ramdisk in config\n");
Peter Tyser41266c92008-08-05 10:51:57 -0500888 return 0;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100889 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100890 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100891#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100892
893 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000894 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100895
896 /*
897 * Check if there is an initrd image at the
898 * address provided in the second bootm argument
899 * check image type, for FIT images get FIT node.
900 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000901 switch (genimg_get_format((void *)rd_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100902 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000903 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100904 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100905
Simon Glass770605e2012-02-13 13:51:18 +0000906 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000907 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100908 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100909
Marian Balakowiczc8779642008-03-12 10:12:37 +0100910 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600911 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600912
Stephen Warren712fbcf2011-10-18 11:11:49 +0000913 rd_data = image_get_data(rd_hdr);
914 rd_len = image_get_data_size(rd_hdr);
915 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100916 break;
917#if defined(CONFIG_FIT)
918 case IMAGE_FORMAT_FIT:
919 fit_hdr = (void *)rd_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000920 printf("## Loading init Ramdisk from FIT "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100921 "Image at %08lx ...\n", rd_addr);
922
Simon Glass770605e2012-02-13 13:51:18 +0000923 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000924 if (!fit_check_format(fit_hdr)) {
925 puts("Bad FIT ramdisk image format!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000926 bootstage_error(
Simon Glassaacc8c12011-12-10 11:08:02 +0000927 BOOTSTAGE_ID_FIT_RD_FORMAT);
Michal Simekc78fce62008-07-11 10:43:13 +0200928 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100929 }
Simon Glass770605e2012-02-13 13:51:18 +0000930 bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100931
932 if (!fit_uname_ramdisk) {
933 /*
934 * no ramdisk image node unit name, try to get config
935 * node first. If config unit node name is NULL
936 * fit_conf_get_node() will try to find default config node
937 */
Simon Glass770605e2012-02-13 13:51:18 +0000938 bootstage_mark(
Simon Glassaacc8c12011-12-10 11:08:02 +0000939 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000940 cfg_noffset = fit_conf_get_node(fit_hdr,
941 fit_uname_config);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100942 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000943 puts("Could not find configuration "
944 "node\n");
Simon Glass770605e2012-02-13 13:51:18 +0000945 bootstage_error(
Simon Glassaacc8c12011-12-10 11:08:02 +0000946 BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
Michal Simekc78fce62008-07-11 10:43:13 +0200947 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100948 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000949 fit_uname_config = fdt_get_name(fit_hdr,
950 cfg_noffset, NULL);
951 printf(" Using '%s' configuration\n",
952 fit_uname_config);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100953
Stephen Warren712fbcf2011-10-18 11:11:49 +0000954 rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
955 cfg_noffset);
956 fit_uname_ramdisk = fit_get_name(fit_hdr,
957 rd_noffset, NULL);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100958 } else {
959 /* get ramdisk component image node offset */
Simon Glass770605e2012-02-13 13:51:18 +0000960 bootstage_mark(
Simon Glassaacc8c12011-12-10 11:08:02 +0000961 BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000962 rd_noffset = fit_image_get_node(fit_hdr,
963 fit_uname_ramdisk);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100964 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100965 if (rd_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000966 puts("Could not find subimage node\n");
Simon Glass770605e2012-02-13 13:51:18 +0000967 bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
Michal Simekc78fce62008-07-11 10:43:13 +0200968 return 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100969 }
Marian Balakowiczc8779642008-03-12 10:12:37 +0100970
Stephen Warren712fbcf2011-10-18 11:11:49 +0000971 printf(" Trying '%s' ramdisk subimage\n",
972 fit_uname_ramdisk);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100973
Simon Glass770605e2012-02-13 13:51:18 +0000974 bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000975 if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
976 images->verify))
Michal Simekc78fce62008-07-11 10:43:13 +0200977 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100978
979 /* get ramdisk image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000980 if (fit_image_get_data(fit_hdr, rd_noffset, &data,
981 &size)) {
982 puts("Could not find ramdisk subimage data!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000983 bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
Michal Simekc78fce62008-07-11 10:43:13 +0200984 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100985 }
Simon Glass770605e2012-02-13 13:51:18 +0000986 bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100987
988 rd_data = (ulong)data;
989 rd_len = size;
990
Stephen Warren712fbcf2011-10-18 11:11:49 +0000991 if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
992 puts("Can't get ramdisk subimage load "
993 "address!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000994 bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
Michal Simekc78fce62008-07-11 10:43:13 +0200995 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100996 }
Simon Glass770605e2012-02-13 13:51:18 +0000997 bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100998
999 images->fit_hdr_rd = fit_hdr;
1000 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +01001001 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +01001002 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001003#endif
1004 default:
Marek Vasut017e1f32012-03-18 11:47:58 +00001005#ifdef CONFIG_SUPPORT_RAW_INITRD
1006 if (argc >= 3 && (end = strchr(argv[2], ':'))) {
1007 rd_len = simple_strtoul(++end, NULL, 16);
1008 rd_data = rd_addr;
1009 } else
1010#endif
1011 {
1012 puts("Wrong Ramdisk Image Format\n");
1013 rd_data = rd_len = rd_load = 0;
1014 return 1;
1015 }
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001016 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001017 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001018 image_check_type(&images->legacy_hdr_os_copy,
1019 IH_TYPE_MULTI)) {
1020
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001021 /*
1022 * Now check if we have a legacy mult-component image,
1023 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001024 */
Simon Glass770605e2012-02-13 13:51:18 +00001025 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001026 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001027 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001028 (ulong)images->legacy_hdr_os);
1029
Stephen Warren712fbcf2011-10-18 11:11:49 +00001030 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001031 } else {
1032 /*
1033 * no initrd image
1034 */
Simon Glass770605e2012-02-13 13:51:18 +00001035 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001036 rd_len = rd_data = 0;
1037 }
1038
1039 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001040 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001041 } else {
1042 *rd_start = rd_data;
1043 *rd_end = rd_data + rd_len;
1044 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001045 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001046 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001047
1048 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001049}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001050
John Rigbyfca43cc2010-10-13 13:57:35 -06001051#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001052/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001053 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001054 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001055 * @rd_data: ramdisk data start address
1056 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001057 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1058 * start address (after possible relocation)
1059 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1060 * end address (after possible relocation)
1061 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001062 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001063 * variable and if requested ramdisk data is moved to a specified location.
1064 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001065 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1066 * start/end addresses if ramdisk image start and len were provided,
1067 * otherwise set initrd_start and initrd_end set to zeros.
1068 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001069 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001070 * 0 - success
1071 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001072 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001073int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001074 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001075{
1076 char *s;
1077 ulong initrd_high;
1078 int initrd_copy_to_ram = 1;
1079
Stephen Warren712fbcf2011-10-18 11:11:49 +00001080 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001081 /* a value of "no" or a similar string will act like 0,
1082 * turning the "load high" feature off. This is intentional.
1083 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001084 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001085 if (initrd_high == ~0)
1086 initrd_copy_to_ram = 0;
1087 } else {
1088 /* not set, no restrictions to load high */
1089 initrd_high = ~0;
1090 }
1091
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001092
1093#ifdef CONFIG_LOGBUFFER
1094 /* Prevent initrd from overwriting logbuffer */
1095 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1096#endif
1097
Stephen Warren712fbcf2011-10-18 11:11:49 +00001098 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001099 initrd_high, initrd_copy_to_ram);
1100
1101 if (rd_data) {
1102 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001103 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001104 *initrd_start = rd_data;
1105 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001106 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001107 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001108 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001109 *initrd_start = (ulong)lmb_alloc_base(lmb,
1110 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001111 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001112 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1113 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001114
Kumar Galae822d7f2008-02-27 21:51:49 -06001115 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001116 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001117 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001118 }
Simon Glass770605e2012-02-13 13:51:18 +00001119 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001120
1121 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001122 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001123 *initrd_start, *initrd_end);
1124
Stephen Warren712fbcf2011-10-18 11:11:49 +00001125 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001126 (void *)rd_data, rd_len, CHUNKSZ);
1127
Kumar Gala3b200112011-12-07 04:42:58 +00001128#ifdef CONFIG_MP
1129 /*
1130 * Ensure the image is flushed to memory to handle
1131 * AMP boot scenarios in which we might not be
1132 * HW cache coherent
1133 */
1134 flush_cache((unsigned long)*initrd_start, rd_len);
1135#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001136 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001137 }
1138 } else {
1139 *initrd_start = 0;
1140 *initrd_end = 0;
1141 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001142 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001143 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001144
Kumar Galae822d7f2008-02-27 21:51:49 -06001145 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001146
Kumar Galae822d7f2008-02-27 21:51:49 -06001147error:
1148 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001149}
John Rigbyfca43cc2010-10-13 13:57:35 -06001150#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001151
Kumar Gala06a09912008-08-15 08:24:38 -05001152#ifdef CONFIG_OF_LIBFDT
Stephen Warren712fbcf2011-10-18 11:11:49 +00001153static void fdt_error(const char *msg)
Kumar Gala06a09912008-08-15 08:24:38 -05001154{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001155 puts("ERROR: ");
1156 puts(msg);
1157 puts(" - must RESET the board to recover.\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001158}
1159
Stephen Warren712fbcf2011-10-18 11:11:49 +00001160static const image_header_t *image_get_fdt(ulong fdt_addr)
Kumar Gala06a09912008-08-15 08:24:38 -05001161{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001162 const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001163
Stephen Warren712fbcf2011-10-18 11:11:49 +00001164 image_print_contents(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001165
Stephen Warren712fbcf2011-10-18 11:11:49 +00001166 puts(" Verifying Checksum ... ");
1167 if (!image_check_hcrc(fdt_hdr)) {
1168 fdt_error("fdt header checksum invalid");
Kumar Gala06a09912008-08-15 08:24:38 -05001169 return NULL;
1170 }
1171
Stephen Warren712fbcf2011-10-18 11:11:49 +00001172 if (!image_check_dcrc(fdt_hdr)) {
1173 fdt_error("fdt checksum invalid");
Kumar Gala06a09912008-08-15 08:24:38 -05001174 return NULL;
1175 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001176 puts("OK\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001177
Stephen Warren712fbcf2011-10-18 11:11:49 +00001178 if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
1179 fdt_error("uImage is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001180 return NULL;
1181 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001182 if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
1183 fdt_error("uImage is compressed");
Kumar Gala06a09912008-08-15 08:24:38 -05001184 return NULL;
1185 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001186 if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
1187 fdt_error("uImage data is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001188 return NULL;
1189 }
1190 return fdt_hdr;
1191}
1192
1193/**
1194 * fit_check_fdt - verify FIT format FDT subimage
1195 * @fit_hdr: pointer to the FIT header
1196 * fdt_noffset: FDT subimage node offset within FIT image
1197 * @verify: data CRC verification flag
1198 *
1199 * fit_check_fdt() verifies integrity of the FDT subimage and from
1200 * specified FIT image.
1201 *
1202 * returns:
1203 * 1, on success
1204 * 0, on failure
1205 */
1206#if defined(CONFIG_FIT)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001207static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
Kumar Gala06a09912008-08-15 08:24:38 -05001208{
Stephen Warren712fbcf2011-10-18 11:11:49 +00001209 fit_image_print(fit, fdt_noffset, " ");
Kumar Gala06a09912008-08-15 08:24:38 -05001210
1211 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001212 puts(" Verifying Hash Integrity ... ");
Simon Glassb8da8362013-05-07 06:11:57 +00001213 if (!fit_image_verify(fit, fdt_noffset)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001214 fdt_error("Bad Data Hash");
Kumar Gala06a09912008-08-15 08:24:38 -05001215 return 0;
1216 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001217 puts("OK\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001218 }
1219
Stephen Warren712fbcf2011-10-18 11:11:49 +00001220 if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
1221 fdt_error("Not a FDT image");
Kumar Gala06a09912008-08-15 08:24:38 -05001222 return 0;
1223 }
1224
Stephen Warren712fbcf2011-10-18 11:11:49 +00001225 if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
1226 fdt_error("FDT image is compressed");
Kumar Gala06a09912008-08-15 08:24:38 -05001227 return 0;
1228 }
1229
1230 return 1;
1231}
1232#endif /* CONFIG_FIT */
1233
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001234#ifndef CONFIG_SYS_FDT_PAD
1235#define CONFIG_SYS_FDT_PAD 0x3000
Kumar Gala06a09912008-08-15 08:24:38 -05001236#endif
1237
Grant Likely55b0a392011-03-28 09:59:01 +00001238#if defined(CONFIG_OF_LIBFDT)
1239/**
1240 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
1241 * @lmb: pointer to lmb handle, will be used for memory mgmt
1242 * @fdt_blob: pointer to fdt blob base address
1243 *
1244 * Adds the memreserve regions in the dtb to the lmb block. Adding the
1245 * memreserve regions prevents u-boot from using them to store the initrd
1246 * or the fdt blob.
1247 */
1248void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
1249{
1250 uint64_t addr, size;
1251 int i, total;
1252
Stephen Warren712fbcf2011-10-18 11:11:49 +00001253 if (fdt_check_header(fdt_blob) != 0)
Grant Likely55b0a392011-03-28 09:59:01 +00001254 return;
1255
1256 total = fdt_num_mem_rsv(fdt_blob);
1257 for (i = 0; i < total; i++) {
1258 if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
1259 continue;
1260 printf(" reserving fdt memory region: addr=%llx size=%llx\n",
1261 (unsigned long long)addr, (unsigned long long)size);
1262 lmb_reserve(lmb, addr, size);
1263 }
1264}
1265
Kumar Gala06a09912008-08-15 08:24:38 -05001266/**
1267 * boot_relocate_fdt - relocate flat device tree
1268 * @lmb: pointer to lmb handle, will be used for memory mgmt
Kumar Gala06a09912008-08-15 08:24:38 -05001269 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1270 * @of_size: pointer to a ulong variable, will hold fdt length
1271 *
Timur Tabi43b08af2010-05-24 15:10:25 -05001272 * boot_relocate_fdt() allocates a region of memory within the bootmap and
1273 * relocates the of_flat_tree into that region, even if the fdt is already in
1274 * the bootmap. It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
1275 * bytes.
Kumar Gala06a09912008-08-15 08:24:38 -05001276 *
1277 * of_flat_tree and of_size are set to final (after relocation) values
1278 *
1279 * returns:
1280 * 0 - success
1281 * 1 - failure
1282 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001283int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001284{
Timur Tabi43b08af2010-05-24 15:10:25 -05001285 void *fdt_blob = *of_flat_tree;
Kim Phillips199adb62012-10-29 13:34:32 +00001286 void *of_start = NULL;
David A. Longa28afca2011-07-09 16:40:19 -04001287 char *fdt_high;
Kumar Gala06a09912008-08-15 08:24:38 -05001288 ulong of_len = 0;
Timur Tabi43b08af2010-05-24 15:10:25 -05001289 int err;
David A. Longa28afca2011-07-09 16:40:19 -04001290 int disable_relocation = 0;
Kumar Gala06a09912008-08-15 08:24:38 -05001291
1292 /* nothing to do */
1293 if (*of_size == 0)
1294 return 0;
1295
Stephen Warren712fbcf2011-10-18 11:11:49 +00001296 if (fdt_check_header(fdt_blob) != 0) {
1297 fdt_error("image is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001298 goto error;
1299 }
1300
Timur Tabi43b08af2010-05-24 15:10:25 -05001301 /* position on a 4K boundary before the alloc_current */
1302 /* Pad the FDT by a specified amount */
1303 of_len = *of_size + CONFIG_SYS_FDT_PAD;
David A. Longa28afca2011-07-09 16:40:19 -04001304
1305 /* If fdt_high is set use it to select the relocation address */
1306 fdt_high = getenv("fdt_high");
1307 if (fdt_high) {
1308 void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);
1309
1310 if (((ulong) desired_addr) == ~0UL) {
1311 /* All ones means use fdt in place */
Shawn Guofa34f6b2012-01-09 21:54:08 +00001312 of_start = fdt_blob;
1313 lmb_reserve(lmb, (ulong)of_start, of_len);
David A. Longa28afca2011-07-09 16:40:19 -04001314 disable_relocation = 1;
Shawn Guofa34f6b2012-01-09 21:54:08 +00001315 } else if (desired_addr) {
David A. Longa28afca2011-07-09 16:40:19 -04001316 of_start =
1317 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
Shawn Guofa34f6b2012-01-09 21:54:08 +00001318 (ulong)desired_addr);
Kim Phillips199adb62012-10-29 13:34:32 +00001319 if (of_start == NULL) {
David A. Longa28afca2011-07-09 16:40:19 -04001320 puts("Failed using fdt_high value for Device Tree");
1321 goto error;
1322 }
1323 } else {
1324 of_start =
Matthew McClintock1bb5e902011-07-18 13:08:05 +00001325 (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
David A. Longa28afca2011-07-09 16:40:19 -04001326 }
1327 } else {
1328 of_start =
1329 (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1330 getenv_bootm_mapsize()
1331 + getenv_bootm_low());
1332 }
Kumar Gala06a09912008-08-15 08:24:38 -05001333
Kim Phillips199adb62012-10-29 13:34:32 +00001334 if (of_start == NULL) {
Timur Tabi43b08af2010-05-24 15:10:25 -05001335 puts("device tree - allocation error\n");
1336 goto error;
Kumar Gala06a09912008-08-15 08:24:38 -05001337 }
1338
David A. Longa28afca2011-07-09 16:40:19 -04001339 if (disable_relocation) {
1340 /* We assume there is space after the existing fdt to use for padding */
1341 fdt_set_totalsize(of_start, of_len);
1342 printf(" Using Device Tree in place at %p, end %p\n",
1343 of_start, of_start + of_len - 1);
1344 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001345 debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
David A. Longa28afca2011-07-09 16:40:19 -04001346 fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
Timur Tabi43b08af2010-05-24 15:10:25 -05001347
Stephen Warren712fbcf2011-10-18 11:11:49 +00001348 printf(" Loading Device Tree to %p, end %p ... ",
David A. Longa28afca2011-07-09 16:40:19 -04001349 of_start, of_start + of_len - 1);
Timur Tabi43b08af2010-05-24 15:10:25 -05001350
Stephen Warren712fbcf2011-10-18 11:11:49 +00001351 err = fdt_open_into(fdt_blob, of_start, of_len);
David A. Longa28afca2011-07-09 16:40:19 -04001352 if (err != 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001353 fdt_error("fdt move failed");
David A. Longa28afca2011-07-09 16:40:19 -04001354 goto error;
1355 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001356 puts("OK\n");
Timur Tabi43b08af2010-05-24 15:10:25 -05001357 }
Timur Tabi43b08af2010-05-24 15:10:25 -05001358
1359 *of_flat_tree = of_start;
1360 *of_size = of_len;
1361
Kumar Gala54f9c862008-08-15 08:24:39 -05001362 set_working_fdt_addr(*of_flat_tree);
Kumar Gala06a09912008-08-15 08:24:38 -05001363 return 0;
1364
1365error:
1366 return 1;
1367}
Grant Likelyed59e582011-03-28 09:58:49 +00001368#endif /* CONFIG_OF_LIBFDT */
Kumar Gala06a09912008-08-15 08:24:38 -05001369
1370/**
1371 * boot_get_fdt - main fdt handling routine
1372 * @argc: command argument count
1373 * @argv: command argument list
1374 * @images: pointer to the bootm images structure
1375 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
1376 * @of_size: pointer to a ulong variable, will hold fdt length
1377 *
1378 * boot_get_fdt() is responsible for finding a valid flat device tree image.
1379 * Curently supported are the following ramdisk sources:
1380 * - multicomponent kernel/ramdisk image,
1381 * - commandline provided address of decicated ramdisk image.
1382 *
1383 * returns:
1384 * 0, if fdt image was found and valid, or skipped
1385 * of_flat_tree and of_size are set to fdt start address and length if
1386 * fdt image is found and valid
1387 *
1388 * 1, if fdt image is found but corrupted
1389 * of_flat_tree and of_size are set to 0 if no fdt exists
1390 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001391int boot_get_fdt(int flag, int argc, char * const argv[],
1392 bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
Kumar Gala06a09912008-08-15 08:24:38 -05001393{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +02001394 const image_header_t *fdt_hdr;
Kumar Gala06a09912008-08-15 08:24:38 -05001395 ulong fdt_addr;
Kumar Gala06a09912008-08-15 08:24:38 -05001396 char *fdt_blob = NULL;
Stephen Warrene37ae402011-11-01 06:28:21 +00001397 ulong image_start, image_data, image_end;
Kumar Gala06a09912008-08-15 08:24:38 -05001398 ulong load_start, load_end;
1399#if defined(CONFIG_FIT)
1400 void *fit_hdr;
1401 const char *fit_uname_config = NULL;
1402 const char *fit_uname_fdt = NULL;
1403 ulong default_addr;
1404 int cfg_noffset;
1405 int fdt_noffset;
1406 const void *data;
1407 size_t size;
1408#endif
1409
1410 *of_flat_tree = NULL;
1411 *of_size = 0;
1412
Stephen Warren712fbcf2011-10-18 11:11:49 +00001413 if (argc > 3 || genimg_has_config(images)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001414#if defined(CONFIG_FIT)
1415 if (argc > 3) {
1416 /*
1417 * If the FDT blob comes from the FIT image and the
1418 * FIT image address is omitted in the command line
1419 * argument, try to use ramdisk or os FIT image
1420 * address or default load address.
1421 */
1422 if (images->fit_uname_rd)
1423 default_addr = (ulong)images->fit_hdr_rd;
1424 else if (images->fit_uname_os)
1425 default_addr = (ulong)images->fit_hdr_os;
1426 else
1427 default_addr = load_addr;
1428
Stephen Warren712fbcf2011-10-18 11:11:49 +00001429 if (fit_parse_conf(argv[3], default_addr,
Kumar Gala06a09912008-08-15 08:24:38 -05001430 &fdt_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001431 debug("* fdt: config '%s' from image at "
1432 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001433 fit_uname_config, fdt_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001434 } else if (fit_parse_subimage(argv[3], default_addr,
Kumar Gala06a09912008-08-15 08:24:38 -05001435 &fdt_addr, &fit_uname_fdt)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001436 debug("* fdt: subimage '%s' from image at "
1437 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001438 fit_uname_fdt, fdt_addr);
1439 } else
1440#endif
1441 {
1442 fdt_addr = simple_strtoul(argv[3], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001443 debug("* fdt: cmdline image address = "
1444 "0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001445 fdt_addr);
1446 }
1447#if defined(CONFIG_FIT)
1448 } else {
1449 /* use FIT configuration provided in first bootm
1450 * command argument
1451 */
1452 fdt_addr = (ulong)images->fit_hdr_os;
1453 fit_uname_config = images->fit_uname_cfg;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001454 debug("* fdt: using config '%s' from image "
1455 "at 0x%08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001456 fit_uname_config, fdt_addr);
1457
1458 /*
1459 * Check whether configuration has FDT blob defined,
1460 * if not quit silently.
1461 */
1462 fit_hdr = (void *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001463 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001464 fit_uname_config);
1465 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001466 debug("* fdt: no such config\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001467 return 0;
1468 }
1469
Stephen Warren712fbcf2011-10-18 11:11:49 +00001470 fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001471 cfg_noffset);
1472 if (fdt_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001473 debug("* fdt: no fdt in config\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001474 return 0;
1475 }
1476 }
1477#endif
1478
Stephen Warren712fbcf2011-10-18 11:11:49 +00001479 debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001480 fdt_addr);
1481
1482 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001483 fdt_addr = genimg_get_image(fdt_addr);
Kumar Gala06a09912008-08-15 08:24:38 -05001484
1485 /*
1486 * Check if there is an FDT image at the
1487 * address provided in the second bootm argument
1488 * check image type, for FIT images get a FIT node.
1489 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001490 switch (genimg_get_format((void *)fdt_addr)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001491 case IMAGE_FORMAT_LEGACY:
1492 /* verify fdt_addr points to a valid image header */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001493 printf("## Flattened Device Tree from Legacy Image "
1494 "at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001495 fdt_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001496 fdt_hdr = image_get_fdt(fdt_addr);
Kumar Gala06a09912008-08-15 08:24:38 -05001497 if (!fdt_hdr)
1498 goto error;
1499
1500 /*
1501 * move image data to the load address,
1502 * make sure we don't overwrite initial image
1503 */
1504 image_start = (ulong)fdt_hdr;
Stephen Warrene37ae402011-11-01 06:28:21 +00001505 image_data = (ulong)image_get_data(fdt_hdr);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001506 image_end = image_get_image_end(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001507
Stephen Warren712fbcf2011-10-18 11:11:49 +00001508 load_start = image_get_load(fdt_hdr);
1509 load_end = load_start + image_get_data_size(fdt_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001510
Stephen Warrene37ae402011-11-01 06:28:21 +00001511 if (load_start == image_start ||
1512 load_start == image_data) {
1513 fdt_blob = (char *)image_data;
1514 break;
1515 }
1516
Kumar Gala06a09912008-08-15 08:24:38 -05001517 if ((load_start < image_end) && (load_end > image_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001518 fdt_error("fdt overwritten");
Kumar Gala06a09912008-08-15 08:24:38 -05001519 goto error;
1520 }
1521
Stephen Warren712fbcf2011-10-18 11:11:49 +00001522 debug(" Loading FDT from 0x%08lx to 0x%08lx\n",
Stephen Warrene37ae402011-11-01 06:28:21 +00001523 image_data, load_start);
Kumar Gala06a09912008-08-15 08:24:38 -05001524
Stephen Warren712fbcf2011-10-18 11:11:49 +00001525 memmove((void *)load_start,
Stephen Warrene37ae402011-11-01 06:28:21 +00001526 (void *)image_data,
Stephen Warren712fbcf2011-10-18 11:11:49 +00001527 image_get_data_size(fdt_hdr));
Kumar Gala06a09912008-08-15 08:24:38 -05001528
1529 fdt_blob = (char *)load_start;
1530 break;
1531 case IMAGE_FORMAT_FIT:
1532 /*
1533 * This case will catch both: new uImage format
1534 * (libfdt based) and raw FDT blob (also libfdt
1535 * based).
1536 */
1537#if defined(CONFIG_FIT)
1538 /* check FDT blob vs FIT blob */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001539 if (fit_check_format((const void *)fdt_addr)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001540 /*
1541 * FIT image
1542 */
1543 fit_hdr = (void *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001544 printf("## Flattened Device Tree from FIT "
1545 "Image at %08lx\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001546 fdt_addr);
1547
1548 if (!fit_uname_fdt) {
1549 /*
1550 * no FDT blob image node unit name,
1551 * try to get config node first. If
1552 * config unit node name is NULL
1553 * fit_conf_get_node() will try to
1554 * find default config node
1555 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001556 cfg_noffset = fit_conf_get_node(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001557 fit_uname_config);
1558
1559 if (cfg_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001560 fdt_error("Could not find "
1561 "configuration "
1562 "node\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001563 goto error;
1564 }
1565
Stephen Warren712fbcf2011-10-18 11:11:49 +00001566 fit_uname_config = fdt_get_name(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001567 cfg_noffset, NULL);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001568 printf(" Using '%s' configuration\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001569 fit_uname_config);
1570
Stephen Warren712fbcf2011-10-18 11:11:49 +00001571 fdt_noffset = fit_conf_get_fdt_node(
1572 fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001573 cfg_noffset);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001574 fit_uname_fdt = fit_get_name(fit_hdr,
Kumar Gala06a09912008-08-15 08:24:38 -05001575 fdt_noffset, NULL);
1576 } else {
1577 /* get FDT component image node offset */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001578 fdt_noffset = fit_image_get_node(
1579 fit_hdr,
1580 fit_uname_fdt);
Kumar Gala06a09912008-08-15 08:24:38 -05001581 }
1582 if (fdt_noffset < 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001583 fdt_error("Could not find subimage "
1584 "node\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001585 goto error;
1586 }
1587
Stephen Warren712fbcf2011-10-18 11:11:49 +00001588 printf(" Trying '%s' FDT blob subimage\n",
Kumar Gala06a09912008-08-15 08:24:38 -05001589 fit_uname_fdt);
1590
Stephen Warren712fbcf2011-10-18 11:11:49 +00001591 if (!fit_check_fdt(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001592 images->verify))
1593 goto error;
1594
1595 /* get ramdisk image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001596 if (fit_image_get_data(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001597 &data, &size)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001598 fdt_error("Could not find FDT "
1599 "subimage data");
Kumar Gala06a09912008-08-15 08:24:38 -05001600 goto error;
1601 }
1602
1603 /* verift that image data is a proper FDT blob */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001604 if (fdt_check_header((char *)data) != 0) {
1605 fdt_error("Subimage data is not a FTD");
Kumar Gala06a09912008-08-15 08:24:38 -05001606 goto error;
1607 }
1608
1609 /*
1610 * move image data to the load address,
1611 * make sure we don't overwrite initial image
1612 */
1613 image_start = (ulong)fit_hdr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001614 image_end = fit_get_end(fit_hdr);
Kumar Gala06a09912008-08-15 08:24:38 -05001615
Stephen Warren712fbcf2011-10-18 11:11:49 +00001616 if (fit_image_get_load(fit_hdr, fdt_noffset,
Kumar Gala06a09912008-08-15 08:24:38 -05001617 &load_start) == 0) {
1618 load_end = load_start + size;
1619
1620 if ((load_start < image_end) &&
1621 (load_end > image_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001622 fdt_error("FDT overwritten");
Kumar Gala06a09912008-08-15 08:24:38 -05001623 goto error;
1624 }
1625
Stephen Warren712fbcf2011-10-18 11:11:49 +00001626 printf(" Loading FDT from 0x%08lx "
1627 "to 0x%08lx\n",
1628 (ulong)data,
1629 load_start);
Kumar Gala06a09912008-08-15 08:24:38 -05001630
Stephen Warren712fbcf2011-10-18 11:11:49 +00001631 memmove((void *)load_start,
Kumar Gala06a09912008-08-15 08:24:38 -05001632 (void *)data, size);
1633
1634 fdt_blob = (char *)load_start;
1635 } else {
1636 fdt_blob = (char *)data;
1637 }
1638
1639 images->fit_hdr_fdt = fit_hdr;
1640 images->fit_uname_fdt = fit_uname_fdt;
1641 images->fit_noffset_fdt = fdt_noffset;
1642 break;
1643 } else
1644#endif
1645 {
1646 /*
1647 * FDT blob
1648 */
1649 fdt_blob = (char *)fdt_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001650 debug("* fdt: raw FDT blob\n");
1651 printf("## Flattened Device Tree blob at "
1652 "%08lx\n", (long)fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001653 }
1654 break;
1655 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001656 puts("ERROR: Did not find a cmdline Flattened Device "
1657 "Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001658 goto error;
1659 }
1660
Simon Glass0ec2ce42011-09-23 06:22:04 +00001661 printf(" Booting using the fdt blob at 0x%p\n", fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001662
1663 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001664 image_check_type(&images->legacy_hdr_os_copy,
1665 IH_TYPE_MULTI)) {
Kumar Gala06a09912008-08-15 08:24:38 -05001666
1667 ulong fdt_data, fdt_len;
1668
1669 /*
1670 * Now check if we have a legacy multi-component image,
1671 * get second entry data start address and len.
1672 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001673 printf("## Flattened Device Tree from multi "
Kumar Gala06a09912008-08-15 08:24:38 -05001674 "component Image at %08lX\n",
1675 (ulong)images->legacy_hdr_os);
1676
Stephen Warren712fbcf2011-10-18 11:11:49 +00001677 image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
1678 &fdt_len);
Kumar Gala06a09912008-08-15 08:24:38 -05001679 if (fdt_len) {
1680
1681 fdt_blob = (char *)fdt_data;
Simon Glass0ec2ce42011-09-23 06:22:04 +00001682 printf(" Booting using the fdt at 0x%p\n", fdt_blob);
Kumar Gala06a09912008-08-15 08:24:38 -05001683
Stephen Warren712fbcf2011-10-18 11:11:49 +00001684 if (fdt_check_header(fdt_blob) != 0) {
1685 fdt_error("image is not a fdt");
Kumar Gala06a09912008-08-15 08:24:38 -05001686 goto error;
1687 }
1688
John Rigbyd1263fc2010-10-13 13:57:32 -06001689 if (fdt_totalsize(fdt_blob) != fdt_len) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001690 fdt_error("fdt size != image size");
Kumar Gala06a09912008-08-15 08:24:38 -05001691 goto error;
1692 }
1693 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001694 debug("## No Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001695 return 0;
1696 }
1697 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001698 debug("## No Flattened Device Tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -05001699 return 0;
1700 }
1701
1702 *of_flat_tree = fdt_blob;
John Rigbyd1263fc2010-10-13 13:57:32 -06001703 *of_size = fdt_totalsize(fdt_blob);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001704 debug(" of_flat_tree at 0x%08lx size 0x%08lx\n",
Andrew Klossner52514692008-08-21 07:12:26 -07001705 (ulong)*of_flat_tree, *of_size);
Kumar Gala06a09912008-08-15 08:24:38 -05001706
1707 return 0;
1708
1709error:
Kim Phillips199adb62012-10-29 13:34:32 +00001710 *of_flat_tree = NULL;
Kumar Gala06a09912008-08-15 08:24:38 -05001711 *of_size = 0;
1712 return 1;
1713}
1714#endif /* CONFIG_OF_LIBFDT */
1715
John Rigbyfca43cc2010-10-13 13:57:35 -06001716#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001717/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001718 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001719 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001720 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1721 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1722 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001723 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001724 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001725 * variable is present its contents is copied to allocated kernel
1726 * command line.
1727 *
1728 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001729 * 0 - success
1730 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001731 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001732int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001733{
1734 char *cmdline;
1735 char *s;
1736
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001737 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001738 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001739
1740 if (cmdline == NULL)
1741 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001742
1743 if ((s = getenv("bootargs")) == NULL)
1744 s = "";
1745
1746 strcpy(cmdline, s);
1747
1748 *cmd_start = (ulong) & cmdline[0];
1749 *cmd_end = *cmd_start + strlen(cmdline);
1750
Stephen Warren712fbcf2011-10-18 11:11:49 +00001751 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001752
Kumar Galae822d7f2008-02-27 21:51:49 -06001753 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001754}
John Rigbyfca43cc2010-10-13 13:57:35 -06001755#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001756
John Rigbyfca43cc2010-10-13 13:57:35 -06001757#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001758/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001759 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001760 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001761 * @kbd: double pointer to board info data
1762 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001763 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001764 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1765 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001766 *
1767 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001768 * 0 - success
1769 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001770 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001771int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001772{
Becky Bruce391fd932008-06-09 20:37:18 -05001773 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001774 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001775 if (*kbd == NULL)
1776 return -1;
1777
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001778 **kbd = *(gd->bd);
1779
Stephen Warren712fbcf2011-10-18 11:11:49 +00001780 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001781
1782#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1783 do_bdinfo(NULL, 0, 0, NULL);
1784#endif
1785
Kumar Galae822d7f2008-02-27 21:51:49 -06001786 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001787}
John Rigbyfca43cc2010-10-13 13:57:35 -06001788#endif /* CONFIG_SYS_BOOT_GET_KBD */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001789#endif /* !USE_HOSTCC */