blob: 162b68269d575196c54608949818aa92a45525b8 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01008 */
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01009
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010010#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010011#include <common.h>
12#include <watchdog.h>
13
14#ifdef CONFIG_SHOW_BOOT_PROGRESS
15#include <status_led.h>
16#endif
17
18#ifdef CONFIG_HAS_DATAFLASH
19#include <dataflash.h>
20#endif
21
Marian Balakowicz95d449a2008-05-13 15:53:29 +020022#ifdef CONFIG_LOGBUFFER
23#include <logbuff.h>
24#endif
25
Marian Balakowicz2242f532008-02-21 17:27:41 +010026#include <rtc.h>
Marian Balakowicz2242f532008-02-21 17:27:41 +010027
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -060028#include <environment.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010029#include <image.h>
30
Stephen Warren712fbcf2011-10-18 11:11:49 +000031#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +010032#include <libfdt.h>
33#include <fdt_support.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010034#endif
35
Andy Fleming20a14a42008-04-02 16:19:07 -050036#include <u-boot/md5.h>
Jeroen Hofstee2b9912e2014-06-12 22:27:12 +020037#include <u-boot/sha1.h>
Simon Glassa51ec632013-05-16 13:53:22 +000038#include <asm/errno.h>
Simon Glass35e7b0f2013-05-07 06:12:03 +000039#include <asm/io.h>
Marian Balakowiczc8779642008-03-12 10:12:37 +010040
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010041#ifdef CONFIG_CMD_BDI
Wolfgang Denk54841ab2010-06-28 22:00:46 +020042extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010043#endif
44
45DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010046
Heiko Schocher21d29f72014-05-28 11:33:33 +020047#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Stephen Warren712fbcf2011-10-18 11:11:49 +000048static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +010049 int verify);
Heiko Schocher21d29f72014-05-28 11:33:33 +020050#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010051#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010052#include "mkimage.h"
Andy Fleming20a14a42008-04-02 16:19:07 -050053#include <u-boot/md5.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010054#include <time.h>
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010055#include <image.h>
Marian Balakowicz5dfb5212008-02-29 21:24:06 +010056#endif /* !USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010057
Simon Glass0ccff502013-02-24 17:33:25 +000058#include <u-boot/crc.h>
59
Simon Glass13d06982013-05-08 08:06:01 +000060#ifndef CONFIG_SYS_BARGSIZE
61#define CONFIG_SYS_BARGSIZE 512
62#endif
63
Mike Frysinger7edb1862010-10-20 07:17:39 -040064static const table_entry_t uimage_arch[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +010065 { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
66 { IH_ARCH_ALPHA, "alpha", "Alpha", },
67 { IH_ARCH_ARM, "arm", "ARM", },
68 { IH_ARCH_I386, "x86", "Intel x86", },
69 { IH_ARCH_IA64, "ia64", "IA64", },
70 { IH_ARCH_M68K, "m68k", "M68K", },
71 { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
72 { IH_ARCH_MIPS, "mips", "MIPS", },
73 { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010074 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Ericksone419e122008-05-04 16:45:01 -070075 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010076 { IH_ARCH_PPC, "ppc", "PowerPC", },
77 { IH_ARCH_S390, "s390", "IBM S390", },
78 { IH_ARCH_SH, "sh", "SuperH", },
79 { IH_ARCH_SPARC, "sparc", "SPARC", },
80 { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
81 { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
82 { IH_ARCH_AVR32, "avr32", "AVR32", },
Macpaul Lin64d61462011-10-19 20:41:09 +000083 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson3ddcacc2011-11-26 19:04:50 +000084 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Simon Glass35e7b0f2013-05-07 06:12:03 +000085 { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
David Feng0ae76532013-12-14 11:47:35 +080086 { IH_ARCH_ARM64, "arm64", "AArch64", },
Alexey Brodkinbc5d5422014-02-04 12:56:16 +040087 { IH_ARCH_ARC, "arc", "ARC", },
Simon Glass5bda35c2014-10-10 08:21:57 -060088 { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010089 { -1, "", "", },
90};
91
Mike Frysinger7edb1862010-10-20 07:17:39 -040092static const table_entry_t uimage_os[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +010093 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowicz570abb02008-02-29 15:59:59 +010094 { IH_OS_LINUX, "linux", "Linux", },
95#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
96 { IH_OS_LYNXOS, "lynxos", "LynxOS", },
97#endif
98 { IH_OS_NETBSD, "netbsd", "NetBSD", },
Torkel Lundgren3df61952010-09-28 11:05:36 +020099 { IH_OS_OSE, "ose", "Enea OSE", },
Steven Stallion04d41402013-03-20 06:31:35 +0000100 { IH_OS_PLAN9, "plan9", "Plan 9", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100101 { IH_OS_RTEMS, "rtems", "RTEMS", },
102 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
miao.yan@windriver.com68b15e82013-12-27 16:01:50 +0800103 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100104#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
105 { IH_OS_QNX, "qnx", "QNX", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100106#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500107#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
108 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
109#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100110#ifdef USE_HOSTCC
111 { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
112 { IH_OS_DELL, "dell", "Dell", },
113 { IH_OS_ESIX, "esix", "Esix", },
114 { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
115 { IH_OS_IRIX, "irix", "Irix", },
116 { IH_OS_NCR, "ncr", "NCR", },
117 { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
118 { IH_OS_PSOS, "psos", "pSOS", },
119 { IH_OS_SCO, "sco", "SCO", },
120 { IH_OS_SOLARIS, "solaris", "Solaris", },
121 { IH_OS_SVR4, "svr4", "SVR4", },
122#endif
Marek Vasut67ddd952014-12-16 14:07:21 +0100123#if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
124 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
125#endif
126
Marian Balakowicz570abb02008-02-29 15:59:59 +0100127 { -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", },
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -0400135 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100136 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700137 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic4962e382011-10-17 00:07:43 +0000138 { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",},
139 { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",},
140 { IH_TYPE_INVALID, NULL, "Invalid Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100141 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic4962e382011-10-17 00:07:43 +0000142 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Shaohui Xie5d898a02012-08-10 02:49:35 +0000143 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
Charles Manning832472a2014-03-06 15:40:50 +1300146 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SOCFPGA preloader",},
Marian Balakowicz570abb02008-02-29 15:59:59 +0100147 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000148 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marek Vasutbce88372013-08-26 20:43:33 +0200149 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
Andreas Bießmann7b1a4112014-05-19 14:23:39 +0200150 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
Simon Glass90268b82014-10-19 21:11:24 -0600151 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
Albert ARIBAUD \(3ADEV\)39f520b2015-03-31 11:40:49 +0200152 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100153 { -1, "", "", },
154};
155
Mike Frysinger7edb1862010-10-20 07:17:39 -0400156static const table_entry_t uimage_comp[] = {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100157 { IH_COMP_NONE, "none", "uncompressed", },
158 { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
159 { IH_COMP_GZIP, "gzip", "gzip compressed", },
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200160 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaard20dde482009-11-19 11:37:51 +0100161 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowicz570abb02008-02-29 15:59:59 +0100162 { -1, "", "", },
163};
164
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100165/*****************************************************************************/
166/* Legacy format routines */
167/*****************************************************************************/
Stephen Warren712fbcf2011-10-18 11:11:49 +0000168int image_check_hcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100169{
170 ulong hcrc;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000171 ulong len = image_get_header_size();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100172 image_header_t header;
173
174 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000175 memmove(&header, (char *)hdr, image_get_header_size());
176 image_set_hcrc(&header, 0);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100177
Stephen Warren712fbcf2011-10-18 11:11:49 +0000178 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100179
Stephen Warren712fbcf2011-10-18 11:11:49 +0000180 return (hcrc == image_get_hcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100181}
182
Stephen Warren712fbcf2011-10-18 11:11:49 +0000183int image_check_dcrc(const image_header_t *hdr)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100184{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000185 ulong data = image_get_data(hdr);
186 ulong len = image_get_data_size(hdr);
187 ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100188
Stephen Warren712fbcf2011-10-18 11:11:49 +0000189 return (dcrc == image_get_dcrc(hdr));
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100190}
191
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100192/**
193 * image_multi_count - get component (sub-image) count
194 * @hdr: pointer to the header of the multi component image
195 *
196 * image_multi_count() returns number of components in a multi
197 * component image.
198 *
199 * Note: no checking of the image type is done, caller must pass
200 * a valid multi component image.
201 *
202 * returns:
203 * number of components
204 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000205ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100206{
207 ulong i, count = 0;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100208 uint32_t *size;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100209
210 /* get start of the image payload, which in case of multi
211 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000212 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100213
214 /* count non empty slots */
215 for (i = 0; size[i]; ++i)
216 count++;
217
218 return count;
219}
220
221/**
222 * image_multi_getimg - get component data address and size
223 * @hdr: pointer to the header of the multi component image
224 * @idx: index of the requested component
225 * @data: pointer to a ulong variable, will hold component data address
226 * @len: pointer to a ulong variable, will hold component size
227 *
228 * image_multi_getimg() returns size and data address for the requested
229 * component in a multi component image.
230 *
231 * Note: no checking of the image type is done, caller must pass
232 * a valid multi component image.
233 *
234 * returns:
235 * data address and size of the component, if idx is valid
236 * 0 in data and len, if idx is out of range
237 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000238void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100239 ulong *data, ulong *len)
240{
241 int i;
Marian Balakowiczdf6f1b82008-02-29 16:00:06 +0100242 uint32_t *size;
Nick Spence02b9b222008-05-10 14:02:04 -0700243 ulong offset, count, img_data;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100244
245 /* get number of component */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000246 count = image_multi_count(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100247
248 /* get start of the image payload, which in case of multi
249 * component images that points to a table of component sizes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000250 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100251
252 /* get address of the proper component data start, which means
253 * skipping sizes table (add 1 for last, null entry) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000254 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100255
256 if (idx < count) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000257 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100258 offset = 0;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100259
260 /* go over all indices preceding requested component idx */
261 for (i = 0; i < idx; i++) {
Nick Spence02b9b222008-05-10 14:02:04 -0700262 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000263 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100264 }
265
266 /* calculate idx-th component data address */
Nick Spence02b9b222008-05-10 14:02:04 -0700267 *data = img_data + offset;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100268 } else {
269 *len = 0;
270 *data = 0;
271 }
272}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100273
Stephen Warren712fbcf2011-10-18 11:11:49 +0000274static void image_print_type(const image_header_t *hdr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100275{
276 const char *os, *arch, *type, *comp;
277
Stephen Warren712fbcf2011-10-18 11:11:49 +0000278 os = genimg_get_os_name(image_get_os(hdr));
279 arch = genimg_get_arch_name(image_get_arch(hdr));
280 type = genimg_get_type_name(image_get_type(hdr));
281 comp = genimg_get_comp_name(image_get_comp(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100282
Stephen Warren712fbcf2011-10-18 11:11:49 +0000283 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100284}
285
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100286/**
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200287 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200288 * @ptr: pointer to the legacy format image header
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100289 * @p: pointer to prefix string
290 *
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200291 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicz5dfb5212008-02-29 21:24:06 +0100292 * The routine prints out all header fields followed by the size/offset data
293 * for MULTI/SCRIPT images.
294 *
295 * returns:
296 * no returned results
297 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000298void image_print_contents(const void *ptr)
Marian Balakowicz2242f532008-02-21 17:27:41 +0100299{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200300 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200301 const char *p;
302
Simon Glass1fe7d932013-05-08 08:05:58 +0000303 p = IMAGE_INDENT_STRING;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000304 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Simon Glass859e92b2013-05-07 06:11:51 +0000305 if (IMAGE_ENABLE_TIMESTAMP) {
306 printf("%sCreated: ", p);
307 genimg_print_time((time_t)image_get_time(hdr));
308 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000309 printf("%sImage Type: ", p);
310 image_print_type(hdr);
311 printf("%sData Size: ", p);
312 genimg_print_size(image_get_data_size(hdr));
313 printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
314 printf("%sEntry Point: %08x\n", p, image_get_ep(hdr));
Marian Balakowicz2242f532008-02-21 17:27:41 +0100315
Stephen Warren712fbcf2011-10-18 11:11:49 +0000316 if (image_check_type(hdr, IH_TYPE_MULTI) ||
317 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowicz2242f532008-02-21 17:27:41 +0100318 int i;
319 ulong data, len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000320 ulong count = image_multi_count(hdr);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100321
Stephen Warren712fbcf2011-10-18 11:11:49 +0000322 printf("%sContents:\n", p);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100323 for (i = 0; i < count; i++) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000324 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100325
Stephen Warren712fbcf2011-10-18 11:11:49 +0000326 printf("%s Image %d: ", p, i);
327 genimg_print_size(len);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100328
Stephen Warren712fbcf2011-10-18 11:11:49 +0000329 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowicz570abb02008-02-29 15:59:59 +0100330 /*
331 * the user may need to know offsets
332 * if planning to do something with
333 * multiple files
334 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000335 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100336 }
Marian Balakowicz2242f532008-02-21 17:27:41 +0100337 }
338 }
339}
340
Marian Balakowicz570abb02008-02-29 15:59:59 +0100341
342#ifndef USE_HOSTCC
Heiko Schocher21d29f72014-05-28 11:33:33 +0200343#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100344/**
345 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100346 * @rd_addr: ramdisk image start address
347 * @arch: expected ramdisk architecture
348 * @verify: checksum verification flag
349 *
350 * image_get_ramdisk() returns a pointer to the verified ramdisk image
351 * header. Routine receives image start address and expected architecture
352 * flag. Verification done covers data and header integrity and os/type/arch
353 * fields checking.
354 *
355 * If dataflash support is enabled routine checks for dataflash addresses
356 * and handles required dataflash reads.
357 *
358 * returns:
359 * pointer to a ramdisk image header, if image was found and valid
Kumar Gala274cea22008-02-27 21:51:46 -0600360 * otherwise, return NULL
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100361 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000362static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100363 int verify)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100364{
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200365 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100366
Stephen Warren712fbcf2011-10-18 11:11:49 +0000367 if (!image_check_magic(rd_hdr)) {
368 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000369 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Kumar Gala274cea22008-02-27 21:51:46 -0600370 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100371 }
372
Stephen Warren712fbcf2011-10-18 11:11:49 +0000373 if (!image_check_hcrc(rd_hdr)) {
374 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000375 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600376 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100377 }
378
Simon Glass770605e2012-02-13 13:51:18 +0000379 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000380 image_print_contents(rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100381
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100382 if (verify) {
383 puts(" Verifying Checksum ... ");
Stephen Warren712fbcf2011-10-18 11:11:49 +0000384 if (!image_check_dcrc(rd_hdr)) {
385 puts("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000386 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Kumar Gala274cea22008-02-27 21:51:46 -0600387 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100388 }
389 puts("OK\n");
390 }
391
Simon Glass770605e2012-02-13 13:51:18 +0000392 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100393
Stephen Warren712fbcf2011-10-18 11:11:49 +0000394 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
395 !image_check_arch(rd_hdr, arch) ||
396 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
397 printf("No Linux %s Ramdisk Image\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100398 genimg_get_arch_name(arch));
Simon Glass770605e2012-02-13 13:51:18 +0000399 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Kumar Gala274cea22008-02-27 21:51:46 -0600400 return NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100401 }
402
403 return rd_hdr;
404}
Heiko Schocher21d29f72014-05-28 11:33:33 +0200405#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100406#endif /* !USE_HOSTCC */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100407
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100408/*****************************************************************************/
409/* Shared dual-format routines */
410/*****************************************************************************/
Marian Balakowicz570abb02008-02-29 15:59:59 +0100411#ifndef USE_HOSTCC
Joe Hershberger1cf0a8b2012-12-11 22:16:28 -0600412ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
413ulong save_addr; /* Default Save Address */
414ulong save_size; /* Default Save Size (in bytes) */
415
416static int on_loadaddr(const char *name, const char *value, enum env_op op,
417 int flags)
418{
419 switch (op) {
420 case env_op_create:
421 case env_op_overwrite:
422 load_addr = simple_strtoul(value, NULL, 16);
423 break;
424 default:
425 break;
426 }
427
428 return 0;
429}
430U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
431
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100432ulong getenv_bootm_low(void)
433{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000434 char *s = getenv("bootm_low");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100435 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000436 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100437 return tmp;
438 }
439
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200440#if defined(CONFIG_SYS_SDRAM_BASE)
441 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100442#elif defined(CONFIG_ARM)
443 return gd->bd->bi_dram[0].start;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100444#else
445 return 0;
446#endif
447}
448
Becky Bruce391fd932008-06-09 20:37:18 -0500449phys_size_t getenv_bootm_size(void)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100450{
Matthew McClintockc519fac2010-07-08 10:11:08 -0500451 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000452 char *s = getenv("bootm_size");
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100453 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000454 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100455 return tmp;
456 }
Matthew McClintockc519fac2010-07-08 10:11:08 -0500457 s = getenv("bootm_low");
458 if (s)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000459 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintockc519fac2010-07-08 10:11:08 -0500460 else
461 tmp = 0;
462
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100463
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100464#if defined(CONFIG_ARM)
Matthew McClintockc519fac2010-07-08 10:11:08 -0500465 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100466#else
Matthew McClintockc519fac2010-07-08 10:11:08 -0500467 return gd->bd->bi_memsize - tmp;
Marian Balakowiczafe45c82008-03-12 12:14:15 +0100468#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100469}
470
Grant Likelyc3624e62011-03-28 09:58:43 +0000471phys_size_t getenv_bootm_mapsize(void)
472{
473 phys_size_t tmp;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000474 char *s = getenv("bootm_mapsize");
Grant Likelyc3624e62011-03-28 09:58:43 +0000475 if (s) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000476 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likelyc3624e62011-03-28 09:58:43 +0000477 return tmp;
478 }
479
480#if defined(CONFIG_SYS_BOOTMAPSZ)
481 return CONFIG_SYS_BOOTMAPSZ;
482#else
483 return getenv_bootm_size();
484#endif
485}
486
Stephen Warren712fbcf2011-10-18 11:11:49 +0000487void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100488{
Larry Johnson54fa2c52010-04-20 08:09:43 -0400489 if (to == from)
490 return;
491
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100492#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800493 if (to > from) {
494 from += len;
495 to += len;
496 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100497 while (len > 0) {
498 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000499 WATCHDOG_RESET();
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800500 if (to > from) {
501 to -= tail;
502 from -= tail;
503 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000504 memmove(to, from, tail);
Sonic Zhang22cfddc2014-12-10 18:20:53 +0800505 if (to < from) {
506 to += tail;
507 from += tail;
508 }
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100509 len -= tail;
510 }
511#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000512 memmove(to, from, len);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100513#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
514}
Marian Balakowicz570abb02008-02-29 15:59:59 +0100515#endif /* !USE_HOSTCC */
516
Stephen Warren712fbcf2011-10-18 11:11:49 +0000517void genimg_print_size(uint32_t size)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100518{
519#ifndef USE_HOSTCC
Stephen Warren712fbcf2011-10-18 11:11:49 +0000520 printf("%d Bytes = ", size);
521 print_size(size, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100522#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000523 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100524 size, (double)size / 1.024e3,
525 (double)size / 1.048576e6);
526#endif
527}
528
Simon Glass859e92b2013-05-07 06:11:51 +0000529#if IMAGE_ENABLE_TIMESTAMP
530void genimg_print_time(time_t timestamp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100531{
532#ifndef USE_HOSTCC
533 struct rtc_time tm;
534
Stephen Warren712fbcf2011-10-18 11:11:49 +0000535 to_tm(timestamp, &tm);
536 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowicz570abb02008-02-29 15:59:59 +0100537 tm.tm_year, tm.tm_mon, tm.tm_mday,
538 tm.tm_hour, tm.tm_min, tm.tm_sec);
539#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000540 printf("%s", ctime(&timestamp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100541#endif
542}
Simon Glass859e92b2013-05-07 06:11:51 +0000543#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100544
545/**
546 * get_table_entry_name - translate entry id to long name
547 * @table: pointer to a translation table for entries of a specific type
548 * @msg: message to be returned when translation fails
549 * @id: entry id to be translated
550 *
551 * get_table_entry_name() will go over translation table trying to find
552 * entry that matches given id. If matching entry is found, its long
553 * name is returned to the caller.
554 *
555 * returns:
556 * long entry name if translation succeeds
557 * msg otherwise
558 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400559char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100560{
561 for (; table->id >= 0; ++table) {
562 if (table->id == id)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200563#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Woode3d1ac72009-04-02 16:15:10 -0500564 return table->lname;
565#else
566 return table->lname + gd->reloc_off;
567#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100568 }
569 return (msg);
570}
571
Stephen Warren712fbcf2011-10-18 11:11:49 +0000572const char *genimg_get_os_name(uint8_t os)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100573{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000574 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100575}
576
Stephen Warren712fbcf2011-10-18 11:11:49 +0000577const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100578{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000579 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
580 arch));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100581}
582
Stephen Warren712fbcf2011-10-18 11:11:49 +0000583const char *genimg_get_type_name(uint8_t type)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100584{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000585 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100586}
587
Stephen Warren712fbcf2011-10-18 11:11:49 +0000588const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100589{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000590 return (get_table_entry_name(uimage_comp, "Unknown Compression",
591 comp));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100592}
593
594/**
595 * get_table_entry_id - translate short entry name to id
596 * @table: pointer to a translation table for entries of a specific type
597 * @table_name: to be used in case of error
598 * @name: entry short name to be translated
599 *
600 * get_table_entry_id() will go over translation table trying to find
601 * entry that matches given short name. If matching entry is found,
602 * its id returned to the caller.
603 *
604 * returns:
605 * entry id if translation succeeds
606 * -1 otherwise
607 */
Mike Frysinger7edb1862010-10-20 07:17:39 -0400608int get_table_entry_id(const table_entry_t *table,
Marian Balakowicz570abb02008-02-29 15:59:59 +0100609 const char *table_name, const char *name)
610{
Mike Frysinger7edb1862010-10-20 07:17:39 -0400611 const table_entry_t *t;
Marian Balakowicz570abb02008-02-29 15:59:59 +0100612#ifdef USE_HOSTCC
613 int first = 1;
614
615 for (t = table; t->id >= 0; ++t) {
616 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000617 return(t->id);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100618 }
619
Stephen Warren712fbcf2011-10-18 11:11:49 +0000620 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100621 for (t = table; t->id >= 0; ++t) {
622 if (t->sname == NULL)
623 continue;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000624 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowicz570abb02008-02-29 15:59:59 +0100625 first = 0;
626 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000627 fprintf(stderr, "\n");
Marian Balakowicz570abb02008-02-29 15:59:59 +0100628#else
629 for (t = table; t->id >= 0; ++t) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200630#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Woode3d1ac72009-04-02 16:15:10 -0500631 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200632#else
633 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser521af042009-09-21 11:20:36 -0500634#endif
Marian Balakowicz570abb02008-02-29 15:59:59 +0100635 return (t->id);
636 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000637 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100638#endif /* USE_HOSTCC */
Marian Balakowicz570abb02008-02-29 15:59:59 +0100639 return (-1);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100640}
641
Stephen Warren712fbcf2011-10-18 11:11:49 +0000642int genimg_get_os_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100643{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000644 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100645}
646
Stephen Warren712fbcf2011-10-18 11:11:49 +0000647int genimg_get_arch_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100648{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000649 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100650}
651
Stephen Warren712fbcf2011-10-18 11:11:49 +0000652int genimg_get_type_id(const char *name)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100653{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000654 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100655}
656
Stephen Warren712fbcf2011-10-18 11:11:49 +0000657int genimg_get_comp_id(const char *name)
Marian Balakowicz570abb02008-02-29 15:59:59 +0100658{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000659 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowicz570abb02008-02-29 15:59:59 +0100660}
661
662#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100663/**
Bryan Wu6c454fe2014-08-15 16:51:38 -0700664 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
665 * FIT strings
Bryan Wu0f641402014-07-31 17:39:58 -0700666 * @img_addr: a string might contain real image address
Bryan Wu6c454fe2014-08-15 16:51:38 -0700667 * @fit_uname_config: double pointer to a char, will hold pointer to a
668 * configuration unit name
669 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
670 * name
Bryan Wu0f641402014-07-31 17:39:58 -0700671 *
Bryan Wu6c454fe2014-08-15 16:51:38 -0700672 * genimg_get_kernel_addr_fit get the real kernel start address from a string
Bryan Wu0f641402014-07-31 17:39:58 -0700673 * which is normally the first argv of bootm/bootz
674 *
675 * returns:
676 * kernel start address
677 */
Bryan Wu6c454fe2014-08-15 16:51:38 -0700678ulong genimg_get_kernel_addr_fit(char * const img_addr,
679 const char **fit_uname_config,
680 const char **fit_uname_kernel)
Bryan Wu0f641402014-07-31 17:39:58 -0700681{
Bryan Wu0f641402014-07-31 17:39:58 -0700682 ulong kernel_addr;
683
684 /* find out kernel image address */
685 if (!img_addr) {
686 kernel_addr = load_addr;
687 debug("* kernel: default image load address = 0x%08lx\n",
688 load_addr);
689#if defined(CONFIG_FIT)
690 } else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -0700691 fit_uname_config)) {
Bryan Wu0f641402014-07-31 17:39:58 -0700692 debug("* kernel: config '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -0700693 *fit_uname_config, kernel_addr);
Bryan Wu0f641402014-07-31 17:39:58 -0700694 } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
Bryan Wu6c454fe2014-08-15 16:51:38 -0700695 fit_uname_kernel)) {
Bryan Wu0f641402014-07-31 17:39:58 -0700696 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Bryan Wu6c454fe2014-08-15 16:51:38 -0700697 *fit_uname_kernel, kernel_addr);
Bryan Wu0f641402014-07-31 17:39:58 -0700698#endif
699 } else {
700 kernel_addr = simple_strtoul(img_addr, NULL, 16);
701 debug("* kernel: cmdline image address = 0x%08lx\n",
702 kernel_addr);
703 }
704
705 return kernel_addr;
706}
707
708/**
Bryan Wu6c454fe2014-08-15 16:51:38 -0700709 * genimg_get_kernel_addr() is the simple version of
710 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
711 */
712ulong genimg_get_kernel_addr(char * const img_addr)
713{
714 const char *fit_uname_config = NULL;
715 const char *fit_uname_kernel = NULL;
716
717 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
718 &fit_uname_kernel);
719}
720
721/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100722 * genimg_get_format - get image format type
723 * @img_addr: image start address
724 *
725 * genimg_get_format() checks whether provided address points to a valid
726 * legacy or FIT image.
727 *
728 * New uImage format and FDT blob are based on a libfdt. FDT blob
729 * may be passed directly or embedded in a FIT image. In both situations
730 * genimg_get_format() must be able to dectect libfdt header.
731 *
732 * returns:
733 * image format type or IMAGE_FORMAT_INVALID if no image is present
734 */
Simon Glass35e7b0f2013-05-07 06:12:03 +0000735int genimg_get_format(const void *img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100736{
Heiko Schocher21d29f72014-05-28 11:33:33 +0200737#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200738 const image_header_t *hdr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100739
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200740 hdr = (const image_header_t *)img_addr;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100741 if (image_check_magic(hdr))
Heiko Schocher21d29f72014-05-28 11:33:33 +0200742 return IMAGE_FORMAT_LEGACY;
743#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100744#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Heiko Schocher21d29f72014-05-28 11:33:33 +0200745 if (fdt_check_header(img_addr) == 0)
746 return IMAGE_FORMAT_FIT;
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -0500747#endif
748#ifdef CONFIG_ANDROID_BOOT_IMAGE
Heiko Schocher21d29f72014-05-28 11:33:33 +0200749 if (android_image_check_header(img_addr) == 0)
750 return IMAGE_FORMAT_ANDROID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100751#endif
752
Heiko Schocher21d29f72014-05-28 11:33:33 +0200753 return IMAGE_FORMAT_INVALID;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100754}
755
756/**
757 * genimg_get_image - get image from special storage (if necessary)
758 * @img_addr: image start address
759 *
Guilherme Maciel Ferreira067d1562015-01-15 02:48:06 -0200760 * genimg_get_image() checks if provided image start address is located
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100761 * in a dataflash storage. If so, image is moved to a system RAM memory.
762 *
763 * returns:
764 * image start address after possible relocation from special storage
765 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000766ulong genimg_get_image(ulong img_addr)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100767{
768 ulong ram_addr = img_addr;
769
770#ifdef CONFIG_HAS_DATAFLASH
771 ulong h_size, d_size;
772
Stephen Warren712fbcf2011-10-18 11:11:49 +0000773 if (addr_dataflash(img_addr)) {
Simon Glass35e7b0f2013-05-07 06:12:03 +0000774 void *buf;
775
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100776 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200777 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100778
779 /* get header size */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000780 h_size = image_get_header_size();
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100781#if defined(CONFIG_FIT)
782 if (sizeof(struct fdt_header) > h_size)
783 h_size = sizeof(struct fdt_header);
784#endif
785
786 /* read in header */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000787 debug(" Reading image header from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100788 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
789
Simon Glass35e7b0f2013-05-07 06:12:03 +0000790 buf = map_sysmem(ram_addr, 0);
791 read_dataflash(img_addr, h_size, buf);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100792
793 /* get data size */
Simon Glass35e7b0f2013-05-07 06:12:03 +0000794 switch (genimg_get_format(buf)) {
Heiko Schocher21d29f72014-05-28 11:33:33 +0200795#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100796 case IMAGE_FORMAT_LEGACY:
Simon Glass35e7b0f2013-05-07 06:12:03 +0000797 d_size = image_get_data_size(buf);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000798 debug(" Legacy format image found at 0x%08lx, "
799 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100800 ram_addr, d_size);
801 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200802#endif
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100803#if defined(CONFIG_FIT)
804 case IMAGE_FORMAT_FIT:
Simon Glass35e7b0f2013-05-07 06:12:03 +0000805 d_size = fit_get_size(buf) - h_size;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000806 debug(" FIT/FDT format image found at 0x%08lx, "
807 "size 0x%08lx\n",
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100808 ram_addr, d_size);
809 break;
810#endif
811 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000812 printf(" No valid image found at 0x%08lx\n",
813 img_addr);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100814 return ram_addr;
815 }
816
817 /* read in image data */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000818 debug(" Reading image remaining data from dataflash address "
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100819 "%08lx to RAM address %08lx\n", img_addr + h_size,
820 ram_addr + h_size);
821
Stephen Warren712fbcf2011-10-18 11:11:49 +0000822 read_dataflash(img_addr + h_size, d_size,
Simon Glass35e7b0f2013-05-07 06:12:03 +0000823 (char *)(buf + h_size));
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100824
825 }
826#endif /* CONFIG_HAS_DATAFLASH */
827
828 return ram_addr;
829}
830
831/**
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100832 * fit_has_config - check if there is a valid FIT configuration
833 * @images: pointer to the bootm command headers structure
834 *
835 * fit_has_config() checks if there is a FIT configuration in use
836 * (if FTI support is present).
837 *
838 * returns:
839 * 0, no FIT support or no configuration found
840 * 1, configuration found
841 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000842int genimg_has_config(bootm_headers_t *images)
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100843{
844#if defined(CONFIG_FIT)
845 if (images->fit_uname_cfg)
846 return 1;
847#endif
848 return 0;
849}
850
851/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100852 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100853 * @argc: command argument count
854 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100855 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100856 * @arch: expected ramdisk architecture
857 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
858 * @rd_end: pointer to a ulong variable, will hold ramdisk end
859 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100860 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100861 * Curently supported are the following ramdisk sources:
862 * - multicomponent kernel/ramdisk image,
863 * - commandline provided address of decicated ramdisk image.
864 *
865 * returns:
Marian Balakowiczd985c842008-03-12 10:14:38 +0100866 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100867 * rd_start and rd_end are set to ramdisk start/end addresses if
868 * ramdisk image is found and valid
Marian Balakowiczd985c842008-03-12 10:14:38 +0100869 *
Kumar Galaea86b9e2008-08-29 19:08:29 -0500870 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100871 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100872 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000873int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100874 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100875{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100876 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100877 ulong rd_data, rd_len;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200878#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Wolfgang Denk3a2003f2009-08-19 11:42:56 +0200879 const image_header_t *rd_hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200880#endif
Simon Glass35e7b0f2013-05-07 06:12:03 +0000881 void *buf;
Marek Vasut57d40ab2012-03-30 23:19:10 +0200882#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut017e1f32012-03-18 11:47:58 +0000883 char *end;
Marek Vasut57d40ab2012-03-30 23:19:10 +0200884#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100885#if defined(CONFIG_FIT)
Simon Glassf320a4d2013-07-10 23:08:10 -0700886 const char *fit_uname_config = images->fit_uname_cfg;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100887 const char *fit_uname_ramdisk = NULL;
888 ulong default_addr;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100889 int rd_noffset;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100890#endif
Simon Glass983c72f2013-06-11 11:14:46 -0700891 const char *select = NULL;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100892
Marian Balakowiczc8779642008-03-12 10:12:37 +0100893 *rd_start = 0;
894 *rd_end = 0;
895
Simon Glass983c72f2013-06-11 11:14:46 -0700896 if (argc >= 2)
897 select = argv[1];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100898 /*
899 * Look for a '-' which indicates to ignore the
900 * ramdisk argument
901 */
Simon Glass983c72f2013-06-11 11:14:46 -0700902 if (select && strcmp(select, "-") == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000903 debug("## Skipping init Ramdisk\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100904 rd_len = rd_data = 0;
Simon Glass983c72f2013-06-11 11:14:46 -0700905 } else if (select || genimg_has_config(images)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100906#if defined(CONFIG_FIT)
Simon Glass983c72f2013-06-11 11:14:46 -0700907 if (select) {
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100908 /*
909 * If the init ramdisk comes from the FIT image and
910 * the FIT image address is omitted in the command
911 * line argument, try to use os FIT image address or
912 * default load address.
913 */
914 if (images->fit_uname_os)
915 default_addr = (ulong)images->fit_hdr_os;
916 else
917 default_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100918
Simon Glass983c72f2013-06-11 11:14:46 -0700919 if (fit_parse_conf(select, default_addr,
920 &rd_addr, &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000921 debug("* ramdisk: config '%s' from image at "
922 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100923 fit_uname_config, rd_addr);
Simon Glass983c72f2013-06-11 11:14:46 -0700924 } else if (fit_parse_subimage(select, default_addr,
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100925 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000926 debug("* ramdisk: subimage '%s' from image at "
927 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100928 fit_uname_ramdisk, rd_addr);
929 } else
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100930#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100931 {
Simon Glass983c72f2013-06-11 11:14:46 -0700932 rd_addr = simple_strtoul(select, NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000933 debug("* ramdisk: cmdline image address = "
934 "0x%08lx\n",
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100935 rd_addr);
936 }
937#if defined(CONFIG_FIT)
938 } else {
939 /* use FIT configuration provided in first bootm
Simon Glassa51ec632013-05-16 13:53:22 +0000940 * command argument. If the property is not defined,
941 * quit silently.
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100942 */
Simon Glass35e7b0f2013-05-07 06:12:03 +0000943 rd_addr = map_to_sysmem(images->fit_hdr_os);
Simon Glassa51ec632013-05-16 13:53:22 +0000944 rd_noffset = fit_get_node_from_config(images,
945 FIT_RAMDISK_PROP, rd_addr);
946 if (rd_noffset == -ENOLINK)
Peter Tyser41266c92008-08-05 10:51:57 -0500947 return 0;
Simon Glassa51ec632013-05-16 13:53:22 +0000948 else if (rd_noffset < 0)
949 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100950 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100951#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100952
953 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000954 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100955
956 /*
957 * Check if there is an initrd image at the
958 * address provided in the second bootm argument
959 * check image type, for FIT images get FIT node.
960 */
Simon Glass35e7b0f2013-05-07 06:12:03 +0000961 buf = map_sysmem(rd_addr, 0);
962 switch (genimg_get_format(buf)) {
Heiko Schocher21d29f72014-05-28 11:33:33 +0200963#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100964 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000965 printf("## Loading init Ramdisk from Legacy "
Marian Balakowiczc8779642008-03-12 10:12:37 +0100966 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100967
Simon Glass770605e2012-02-13 13:51:18 +0000968 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000969 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowiczd985c842008-03-12 10:14:38 +0100970 images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100971
Marian Balakowiczc8779642008-03-12 10:12:37 +0100972 if (rd_hdr == NULL)
Kumar Gala274cea22008-02-27 21:51:46 -0600973 return 1;
Kumar Gala274cea22008-02-27 21:51:46 -0600974
Stephen Warren712fbcf2011-10-18 11:11:49 +0000975 rd_data = image_get_data(rd_hdr);
976 rd_len = image_get_data_size(rd_hdr);
977 rd_load = image_get_load(rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100978 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200979#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100980#if defined(CONFIG_FIT)
981 case IMAGE_FORMAT_FIT:
Simon Glass126cc862014-06-12 07:24:47 -0600982 rd_noffset = fit_image_load(images,
Simon Glassa51ec632013-05-16 13:53:22 +0000983 rd_addr, &fit_uname_ramdisk,
Simon Glassf320a4d2013-07-10 23:08:10 -0700984 &fit_uname_config, arch,
Simon Glassa51ec632013-05-16 13:53:22 +0000985 IH_TYPE_RAMDISK,
986 BOOTSTAGE_ID_FIT_RD_START,
Simon Glassfe20a812014-08-22 14:26:43 -0600987 FIT_LOAD_OPTIONAL_NON_ZERO,
988 &rd_data, &rd_len);
Simon Glassa51ec632013-05-16 13:53:22 +0000989 if (rd_noffset < 0)
Michal Simekc78fce62008-07-11 10:43:13 +0200990 return 1;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100991
Simon Glassa51ec632013-05-16 13:53:22 +0000992 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
Marian Balakowiczc8779642008-03-12 10:12:37 +0100993 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100994 images->fit_noffset_rd = rd_noffset;
Marian Balakowiczc8779642008-03-12 10:12:37 +0100995 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100996#endif
997 default:
Marek Vasut017e1f32012-03-18 11:47:58 +0000998#ifdef CONFIG_SUPPORT_RAW_INITRD
Simon Glass983c72f2013-06-11 11:14:46 -0700999 end = NULL;
1000 if (select)
1001 end = strchr(select, ':');
1002 if (end) {
Marek Vasut017e1f32012-03-18 11:47:58 +00001003 rd_len = simple_strtoul(++end, NULL, 16);
1004 rd_data = rd_addr;
1005 } else
1006#endif
1007 {
1008 puts("Wrong Ramdisk Image Format\n");
1009 rd_data = rd_len = rd_load = 0;
1010 return 1;
1011 }
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001012 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001013 } else if (images->legacy_hdr_valid &&
Stephen Warren712fbcf2011-10-18 11:11:49 +00001014 image_check_type(&images->legacy_hdr_os_copy,
1015 IH_TYPE_MULTI)) {
1016
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001017 /*
1018 * Now check if we have a legacy mult-component image,
1019 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001020 */
Simon Glass770605e2012-02-13 13:51:18 +00001021 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001022 printf("## Loading init Ramdisk from multi component "
Marian Balakowiczc8779642008-03-12 10:12:37 +01001023 "Legacy Image at %08lx ...\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001024 (ulong)images->legacy_hdr_os);
1025
Stephen Warren712fbcf2011-10-18 11:11:49 +00001026 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05001027 }
1028#ifdef CONFIG_ANDROID_BOOT_IMAGE
Ahmad Draidi86f46952014-10-23 20:50:07 +03001029 else if ((genimg_get_format((void *)images->os.start)
1030 == IMAGE_FORMAT_ANDROID) &&
Sebastian Siewior9ace3fc2014-05-05 15:08:09 -05001031 (!android_image_get_ramdisk((void *)images->os.start,
1032 &rd_data, &rd_len))) {
1033 /* empty */
1034 }
1035#endif
1036 else {
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001037 /*
1038 * no initrd image
1039 */
Simon Glass770605e2012-02-13 13:51:18 +00001040 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001041 rd_len = rd_data = 0;
1042 }
1043
1044 if (!rd_data) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001045 debug("## No init Ramdisk\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001046 } else {
1047 *rd_start = rd_data;
1048 *rd_end = rd_data + rd_len;
1049 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001050 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001051 *rd_start, *rd_end);
Kumar Gala274cea22008-02-27 21:51:46 -06001052
1053 return 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +01001054}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001055
John Rigbyfca43cc2010-10-13 13:57:35 -06001056#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001057/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001058 * boot_ramdisk_high - relocate init ramdisk
Kumar Galae822d7f2008-02-27 21:51:49 -06001059 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001060 * @rd_data: ramdisk data start address
1061 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001062 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
1063 * start address (after possible relocation)
1064 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
1065 * end address (after possible relocation)
1066 *
Robert P. J. Day1bce2ae2013-09-16 07:15:45 -04001067 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001068 * variable and if requested ramdisk data is moved to a specified location.
1069 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001070 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
1071 * start/end addresses if ramdisk image start and len were provided,
1072 * otherwise set initrd_start and initrd_end set to zeros.
1073 *
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001074 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001075 * 0 - success
1076 * -1 - failure
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001077 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001078int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galae822d7f2008-02-27 21:51:49 -06001079 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001080{
1081 char *s;
1082 ulong initrd_high;
1083 int initrd_copy_to_ram = 1;
1084
Stephen Warren712fbcf2011-10-18 11:11:49 +00001085 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001086 /* a value of "no" or a similar string will act like 0,
1087 * turning the "load high" feature off. This is intentional.
1088 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001089 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001090 if (initrd_high == ~0)
1091 initrd_copy_to_ram = 0;
1092 } else {
1093 /* not set, no restrictions to load high */
1094 initrd_high = ~0;
1095 }
1096
Marian Balakowicz95d449a2008-05-13 15:53:29 +02001097
1098#ifdef CONFIG_LOGBUFFER
1099 /* Prevent initrd from overwriting logbuffer */
1100 lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
1101#endif
1102
Stephen Warren712fbcf2011-10-18 11:11:49 +00001103 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001104 initrd_high, initrd_copy_to_ram);
1105
1106 if (rd_data) {
1107 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001108 debug(" in-place initrd\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001109 *initrd_start = rd_data;
1110 *initrd_end = rd_data + rd_len;
Kumar Galae822d7f2008-02-27 21:51:49 -06001111 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001112 } else {
Kumar Galae822d7f2008-02-27 21:51:49 -06001113 if (initrd_high)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001114 *initrd_start = (ulong)lmb_alloc_base(lmb,
1115 rd_len, 0x1000, initrd_high);
Kumar Galae822d7f2008-02-27 21:51:49 -06001116 else
Stephen Warren712fbcf2011-10-18 11:11:49 +00001117 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1118 0x1000);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001119
Kumar Galae822d7f2008-02-27 21:51:49 -06001120 if (*initrd_start == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001121 puts("ramdisk - allocation error\n");
Kumar Galae822d7f2008-02-27 21:51:49 -06001122 goto error;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001123 }
Simon Glass770605e2012-02-13 13:51:18 +00001124 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001125
1126 *initrd_end = *initrd_start + rd_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001127 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001128 *initrd_start, *initrd_end);
1129
Stephen Warren712fbcf2011-10-18 11:11:49 +00001130 memmove_wd((void *)*initrd_start,
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001131 (void *)rd_data, rd_len, CHUNKSZ);
1132
Kumar Gala3b200112011-12-07 04:42:58 +00001133#ifdef CONFIG_MP
1134 /*
1135 * Ensure the image is flushed to memory to handle
1136 * AMP boot scenarios in which we might not be
1137 * HW cache coherent
1138 */
1139 flush_cache((unsigned long)*initrd_start, rd_len);
1140#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +00001141 puts("OK\n");
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001142 }
1143 } else {
1144 *initrd_start = 0;
1145 *initrd_end = 0;
1146 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001147 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001148 *initrd_start, *initrd_end);
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001149
Kumar Galae822d7f2008-02-27 21:51:49 -06001150 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001151
Kumar Galae822d7f2008-02-27 21:51:49 -06001152error:
1153 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001154}
John Rigbyfca43cc2010-10-13 13:57:35 -06001155#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001156
Simon Glass90268b82014-10-19 21:11:24 -06001157int boot_get_setup(bootm_headers_t *images, uint8_t arch,
1158 ulong *setup_start, ulong *setup_len)
1159{
1160#if defined(CONFIG_FIT)
1161 return boot_get_setup_fit(images, arch, setup_start, setup_len);
1162#else
1163 return -ENOENT;
1164#endif
1165}
1166
John Rigbyfca43cc2010-10-13 13:57:35 -06001167#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001168/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001169 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galae822d7f2008-02-27 21:51:49 -06001170 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001171 * @cmd_start: pointer to a ulong variable, will hold cmdline start
1172 * @cmd_end: pointer to a ulong variable, will hold cmdline end
1173 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001174 * boot_get_cmdline() allocates space for kernel command line below
Grant Likely590d3ca2011-03-28 09:58:34 +00001175 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001176 * variable is present its contents is copied to allocated kernel
1177 * command line.
1178 *
1179 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001180 * 0 - success
1181 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001182 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001183int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001184{
1185 char *cmdline;
1186 char *s;
1187
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001188 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001189 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001190
1191 if (cmdline == NULL)
1192 return -1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001193
1194 if ((s = getenv("bootargs")) == NULL)
1195 s = "";
1196
1197 strcpy(cmdline, s);
1198
1199 *cmd_start = (ulong) & cmdline[0];
1200 *cmd_end = *cmd_start + strlen(cmdline);
1201
Stephen Warren712fbcf2011-10-18 11:11:49 +00001202 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001203
Kumar Galae822d7f2008-02-27 21:51:49 -06001204 return 0;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001205}
John Rigbyfca43cc2010-10-13 13:57:35 -06001206#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001207
John Rigbyfca43cc2010-10-13 13:57:35 -06001208#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001209/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001210 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galae822d7f2008-02-27 21:51:49 -06001211 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001212 * @kbd: double pointer to board info data
1213 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +01001214 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likely590d3ca2011-03-28 09:58:34 +00001215 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
1216 * with the current u-boot board info data.
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001217 *
1218 * returns:
Kumar Galae822d7f2008-02-27 21:51:49 -06001219 * 0 - success
1220 * -1 - failure
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001221 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001222int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001223{
Becky Bruce391fd932008-06-09 20:37:18 -05001224 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likelyc3624e62011-03-28 09:58:43 +00001225 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galae822d7f2008-02-27 21:51:49 -06001226 if (*kbd == NULL)
1227 return -1;
1228
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001229 **kbd = *(gd->bd);
1230
Stephen Warren712fbcf2011-10-18 11:11:49 +00001231 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +01001232
1233#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
1234 do_bdinfo(NULL, 0, 0, NULL);
1235#endif
1236
Kumar Galae822d7f2008-02-27 21:51:49 -06001237 return 0;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +01001238}
John Rigbyfca43cc2010-10-13 13:57:35 -06001239#endif /* CONFIG_SYS_BOOT_GET_KBD */
Simon Glass13d06982013-05-08 08:06:01 +00001240
1241#ifdef CONFIG_LMB
1242int image_setup_linux(bootm_headers_t *images)
1243{
1244 ulong of_size = images->ft_len;
1245 char **of_flat_tree = &images->ft_addr;
1246 ulong *initrd_start = &images->initrd_start;
1247 ulong *initrd_end = &images->initrd_end;
1248 struct lmb *lmb = &images->lmb;
1249 ulong rd_len;
1250 int ret;
1251
1252 if (IMAGE_ENABLE_OF_LIBFDT)
1253 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1254
1255 if (IMAGE_BOOT_GET_CMDLINE) {
1256 ret = boot_get_cmdline(lmb, &images->cmdline_start,
1257 &images->cmdline_end);
1258 if (ret) {
1259 puts("ERROR with allocation of cmdline\n");
1260 return ret;
1261 }
1262 }
1263 if (IMAGE_ENABLE_RAMDISK_HIGH) {
1264 rd_len = images->rd_end - images->rd_start;
1265 ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
1266 initrd_start, initrd_end);
1267 if (ret)
1268 return ret;
1269 }
1270
1271 if (IMAGE_ENABLE_OF_LIBFDT) {
1272 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1273 if (ret)
1274 return ret;
1275 }
1276
1277 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
1278 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
1279 if (ret)
1280 return ret;
1281 }
1282
1283 return 0;
1284}
1285#endif /* CONFIG_LMB */
Marian Balakowicz5dfb5212008-02-29 21:24:06 +01001286#endif /* !USE_HOSTCC */