blob: 162b68269d575196c54608949818aa92a45525b8 [file] [log] [blame]
Marian Balakowicz41d71ed2008-01-08 18:14:09 +01001/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Marian Balakowicz41d71ed2008-01-08 18:14:09 +01008 */
Marian Balakowicz9c701e82008-01-31 13:57:17 +01009
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010010#ifndef USE_HOSTCC
Marian Balakowicz95418502008-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 Balakowicz902ce702008-05-13 15:53:29 +020022#ifdef CONFIG_LOGBUFFER
23#include <logbuff.h>
24#endif
25
Marian Balakowicza1cc1472008-02-21 17:27:41 +010026#include <rtc.h>
Marian Balakowicza1cc1472008-02-21 17:27:41 +010027
Joe Hershbergerd1f5ae92012-12-11 22:16:28 -060028#include <environment.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010029#include <image.h>
30
Stephen Warren6904b6e2011-10-18 11:11:49 +000031#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +010032#include <libfdt.h>
33#include <fdt_support.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010034#endif
35
Andy Fleming72c23be2008-04-02 16:19:07 -050036#include <u-boot/md5.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020037#include <u-boot/sha1.h>
Simon Glass65817a62013-05-16 13:53:22 +000038#include <asm/errno.h>
Simon Glass49648062013-05-07 06:12:03 +000039#include <asm/io.h>
Marian Balakowicza50d5152008-03-12 10:12:37 +010040
Marian Balakowicz2efc2642008-01-31 13:58:13 +010041#ifdef CONFIG_CMD_BDI
Wolfgang Denk6262d0212010-06-28 22:00:46 +020042extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Marian Balakowicz2efc2642008-01-31 13:58:13 +010043#endif
44
45DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz61fde552008-02-27 11:01:04 +010046
Heiko Schocher515eb122014-05-28 11:33:33 +020047#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Stephen Warren6904b6e2011-10-18 11:11:49 +000048static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +010049 int verify);
Heiko Schocher515eb122014-05-28 11:33:33 +020050#endif
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010051#else
Marian Balakowicz95418502008-01-31 13:55:39 +010052#include "mkimage.h"
Andy Fleming72c23be2008-04-02 16:19:07 -050053#include <u-boot/md5.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010054#include <time.h>
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010055#include <image.h>
Marian Balakowicze227fbb2008-02-29 21:24:06 +010056#endif /* !USE_HOSTCC*/
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010057
Simon Glass9adb6862013-02-24 17:33:25 +000058#include <u-boot/crc.h>
59
Simon Glass9ca37422013-05-08 08:06:01 +000060#ifndef CONFIG_SYS_BARGSIZE
61#define CONFIG_SYS_BARGSIZE 512
62#endif
63
Mike Frysingercf2feb12010-10-20 07:17:39 -040064static const table_entry_t uimage_arch[] = {
Marian Balakowiczc3c7eb22008-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 Balakowiczc3c7eb22008-02-29 15:59:59 +010074 { IH_ARCH_NIOS2, "nios2", "NIOS II", },
Grant Erickson80f51b82008-05-04 16:45:01 -070075 { IH_ARCH_PPC, "powerpc", "PowerPC", },
Marian Balakowiczc3c7eb22008-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 Lin354b4e32011-10-19 20:41:09 +000083 { IH_ARCH_NDS32, "nds32", "NDS32", },
Stefan Kristiansson15c669c2011-11-26 19:04:50 +000084 { IH_ARCH_OPENRISC, "or1k", "OpenRISC 1000",},
Simon Glass49648062013-05-07 06:12:03 +000085 { IH_ARCH_SANDBOX, "sandbox", "Sandbox", },
David Feng85fd5f12013-12-14 11:47:35 +080086 { IH_ARCH_ARM64, "arm64", "AArch64", },
Alexey Brodkin78bc86f2014-02-04 12:56:16 +040087 { IH_ARCH_ARC, "arc", "ARC", },
Simon Glass9d428302014-10-10 08:21:57 -060088 { IH_ARCH_X86_64, "x86_64", "AMD x86_64", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010089 { -1, "", "", },
90};
91
Mike Frysingercf2feb12010-10-20 07:17:39 -040092static const table_entry_t uimage_os[] = {
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +010093 { IH_OS_INVALID, NULL, "Invalid OS", },
Marian Balakowiczc3c7eb22008-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 Lundgren29a58662010-09-28 11:05:36 +020099 { IH_OS_OSE, "ose", "Enea OSE", },
Steven Stallion36ec8ac2013-03-20 06:31:35 +0000100 { IH_OS_PLAN9, "plan9", "Plan 9", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100101 { IH_OS_RTEMS, "rtems", "RTEMS", },
102 { IH_OS_U_BOOT, "u-boot", "U-Boot", },
miao.yan@windriver.com8bb64242013-12-27 16:01:50 +0800103 { IH_OS_VXWORKS, "vxworks", "VxWorks", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100104#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
105 { IH_OS_QNX, "qnx", "QNX", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100106#endif
Peter Tyser56b8dd12008-09-08 14:56:49 -0500107#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
108 { IH_OS_INTEGRITY,"integrity", "INTEGRITY", },
109#endif
Marian Balakowiczc3c7eb22008-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 Vasut0e3b5122014-12-16 14:07:21 +0100123#if defined(CONFIG_BOOTM_OPENRTOS) || defined(USE_HOSTCC)
124 { IH_OS_OPENRTOS, "openrtos", "OpenRTOS", },
125#endif
126
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100127 { -1, "", "", },
128};
129
Mike Frysingercf2feb12010-10-20 07:17:39 -0400130static const table_entry_t uimage_type[] = {
Stefano Babic38855512011-10-17 00:07:43 +0000131 { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100132 { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
133 { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
John Rigby34d12a12011-07-21 09:10:30 -0400134 { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400135 { IH_TYPE_GPIMAGE, "gpimage", "TI Keystone SPL Image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100136 { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
Stephen Warren8c87eba2011-11-10 13:17:53 -0700137 { IH_TYPE_KERNEL_NOLOAD, "kernel_noload", "Kernel Image (no loading done)", },
Stefano Babic38855512011-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 Balakowiczc3c7eb22008-02-29 15:59:59 +0100141 { IH_TYPE_MULTI, "multi", "Multi-File Image", },
Stefano Babic38855512011-10-17 00:07:43 +0000142 { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",},
Shaohui Xieea65fd82012-08-10 02:49:35 +0000143 { IH_TYPE_PBLIMAGE, "pblimage", "Freescale PBL Boot Image",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100144 { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
145 { IH_TYPE_SCRIPT, "script", "Script", },
Charles Manning685e87f2014-03-06 15:40:50 +1300146 { IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SOCFPGA preloader",},
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100147 { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
Heiko Schocher58cb84d2011-07-16 00:06:42 +0000148 { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",},
Marek Vasut4a0e05d2013-08-26 20:43:33 +0200149 { IH_TYPE_MXSIMAGE, "mxsimage", "Freescale MXS Boot Image",},
Andreas Bießmann7c75d3e2014-05-19 14:23:39 +0200150 { IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
Simon Glass0129b522014-10-19 21:11:24 -0600151 { IH_TYPE_X86_SETUP, "x86_setup", "x86 setup.bin", },
Albert ARIBAUD \(3ADEV\)ed1cb1a2015-03-31 11:40:49 +0200152 { IH_TYPE_LPC32XXIMAGE, "lpc32xximage", "LPC32XX Boot Image", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100153 { -1, "", "", },
154};
155
Mike Frysingercf2feb12010-10-20 07:17:39 -0400156static const table_entry_t uimage_comp[] = {
Marian Balakowiczc3c7eb22008-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' Mantellini35afc062008-09-08 02:46:13 +0200160 { IH_COMP_LZMA, "lzma", "lzma compressed", },
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100161 { IH_COMP_LZO, "lzo", "lzo compressed", },
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100162 { -1, "", "", },
163};
164
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100165/*****************************************************************************/
166/* Legacy format routines */
167/*****************************************************************************/
Stephen Warren6904b6e2011-10-18 11:11:49 +0000168int image_check_hcrc(const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100169{
170 ulong hcrc;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000171 ulong len = image_get_header_size();
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100172 image_header_t header;
173
174 /* Copy header so we can blank CRC field for re-calculation */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000175 memmove(&header, (char *)hdr, image_get_header_size());
176 image_set_hcrc(&header, 0);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100177
Stephen Warren6904b6e2011-10-18 11:11:49 +0000178 hcrc = crc32(0, (unsigned char *)&header, len);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100179
Stephen Warren6904b6e2011-10-18 11:11:49 +0000180 return (hcrc == image_get_hcrc(hdr));
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100181}
182
Stephen Warren6904b6e2011-10-18 11:11:49 +0000183int image_check_dcrc(const image_header_t *hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100184{
Stephen Warren6904b6e2011-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 Balakowicz41d71ed2008-01-08 18:14:09 +0100188
Stephen Warren6904b6e2011-10-18 11:11:49 +0000189 return (dcrc == image_get_dcrc(hdr));
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100190}
191
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000205ulong image_multi_count(const image_header_t *hdr)
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100206{
207 ulong i, count = 0;
Marian Balakowicza1474212008-02-29 16:00:06 +0100208 uint32_t *size;
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000212 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000238void image_multi_getimg(const image_header_t *hdr, ulong idx,
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100239 ulong *data, ulong *len)
240{
241 int i;
Marian Balakowicza1474212008-02-29 16:00:06 +0100242 uint32_t *size;
Nick Spence143a1592008-05-10 14:02:04 -0700243 ulong offset, count, img_data;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100244
245 /* get number of component */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000246 count = image_multi_count(hdr);
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000250 size = (uint32_t *)image_get_data(hdr);
Marian Balakowiczb4a12a92008-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 Warren6904b6e2011-10-18 11:11:49 +0000254 img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100255
256 if (idx < count) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000257 *len = uimage_to_cpu(size[idx]);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100258 offset = 0;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100259
260 /* go over all indices preceding requested component idx */
261 for (i = 0; i < idx; i++) {
Nick Spence143a1592008-05-10 14:02:04 -0700262 /* add up i-th component size, rounding up to 4 bytes */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000263 offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100264 }
265
266 /* calculate idx-th component data address */
Nick Spence143a1592008-05-10 14:02:04 -0700267 *data = img_data + offset;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100268 } else {
269 *len = 0;
270 *data = 0;
271 }
272}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100273
Stephen Warren6904b6e2011-10-18 11:11:49 +0000274static void image_print_type(const image_header_t *hdr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100275{
276 const char *os, *arch, *type, *comp;
277
Stephen Warren6904b6e2011-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100282
Stephen Warren6904b6e2011-10-18 11:11:49 +0000283 printf("%s %s %s (%s)\n", arch, os, type, comp);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100284}
285
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100286/**
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200287 * image_print_contents - prints out the contents of the legacy format image
Wolfgang Denk3b506772009-08-19 11:42:56 +0200288 * @ptr: pointer to the legacy format image header
Marian Balakowicze227fbb2008-02-29 21:24:06 +0100289 * @p: pointer to prefix string
290 *
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200291 * image_print_contents() formats a multi line legacy image contents description.
Marian Balakowicze227fbb2008-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 Warren6904b6e2011-10-18 11:11:49 +0000298void image_print_contents(const void *ptr)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100299{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200300 const image_header_t *hdr = (const image_header_t *)ptr;
Bartlomiej Sieka47868592008-04-18 12:39:23 +0200301 const char *p;
302
Simon Glass1030f162013-05-08 08:05:58 +0000303 p = IMAGE_INDENT_STRING;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000304 printf("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name(hdr));
Simon Glassaf0ec0d2013-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 Warren6904b6e2011-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100315
Stephen Warren6904b6e2011-10-18 11:11:49 +0000316 if (image_check_type(hdr, IH_TYPE_MULTI) ||
317 image_check_type(hdr, IH_TYPE_SCRIPT)) {
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100318 int i;
319 ulong data, len;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000320 ulong count = image_multi_count(hdr);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100321
Stephen Warren6904b6e2011-10-18 11:11:49 +0000322 printf("%sContents:\n", p);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100323 for (i = 0; i < count; i++) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000324 image_multi_getimg(hdr, i, &data, &len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100325
Stephen Warren6904b6e2011-10-18 11:11:49 +0000326 printf("%s Image %d: ", p, i);
327 genimg_print_size(len);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100328
Stephen Warren6904b6e2011-10-18 11:11:49 +0000329 if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
Marian Balakowiczc3c7eb22008-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 Warren6904b6e2011-10-18 11:11:49 +0000335 printf("%s Offset = 0x%08lx\n", p, data);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100336 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100337 }
338 }
339}
340
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100341
342#ifndef USE_HOSTCC
Heiko Schocher515eb122014-05-28 11:33:33 +0200343#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100344/**
345 * image_get_ramdisk - get and verify ramdisk image
Marian Balakowiczd7c88a42008-02-29 14:58:34 +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
360 * otherwise, return NULL
361 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000362static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100363 int verify)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100364{
Wolfgang Denk3b506772009-08-19 11:42:56 +0200365 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100366
Stephen Warren6904b6e2011-10-18 11:11:49 +0000367 if (!image_check_magic(rd_hdr)) {
368 puts("Bad Magic Number\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000369 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100370 return NULL;
371 }
372
Stephen Warren6904b6e2011-10-18 11:11:49 +0000373 if (!image_check_hcrc(rd_hdr)) {
374 puts("Bad Header Checksum\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000375 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100376 return NULL;
377 }
378
Simon Glass0169e6b2012-02-13 13:51:18 +0000379 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000380 image_print_contents(rd_hdr);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100381
382 if (verify) {
383 puts(" Verifying Checksum ... ");
Stephen Warren6904b6e2011-10-18 11:11:49 +0000384 if (!image_check_dcrc(rd_hdr)) {
385 puts("Bad Data CRC\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000386 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100387 return NULL;
388 }
389 puts("OK\n");
390 }
391
Simon Glass0169e6b2012-02-13 13:51:18 +0000392 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100393
Stephen Warren6904b6e2011-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100398 genimg_get_arch_name(arch));
Simon Glass0169e6b2012-02-13 13:51:18 +0000399 bootstage_error(BOOTSTAGE_ID_RAMDISK);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100400 return NULL;
401 }
402
403 return rd_hdr;
404}
Heiko Schocher515eb122014-05-28 11:33:33 +0200405#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100406#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100407
408/*****************************************************************************/
409/* Shared dual-format routines */
410/*****************************************************************************/
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100411#ifndef USE_HOSTCC
Joe Hershbergerd1f5ae92012-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100432ulong getenv_bootm_low(void)
433{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000434 char *s = getenv("bootm_low");
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100435 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000436 ulong tmp = simple_strtoul(s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100437 return tmp;
438 }
439
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200440#if defined(CONFIG_SYS_SDRAM_BASE)
441 return CONFIG_SYS_SDRAM_BASE;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100442#elif defined(CONFIG_ARM)
443 return gd->bd->bi_dram[0].start;
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100444#else
445 return 0;
446#endif
447}
448
Becky Bruced26d67c2008-06-09 20:37:18 -0500449phys_size_t getenv_bootm_size(void)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100450{
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500451 phys_size_t tmp;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000452 char *s = getenv("bootm_size");
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100453 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000454 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100455 return tmp;
456 }
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500457 s = getenv("bootm_low");
458 if (s)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000459 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500460 else
461 tmp = 0;
462
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100463
Marian Balakowicz2499b042008-03-12 12:14:15 +0100464#if defined(CONFIG_ARM)
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500465 return gd->bd->bi_dram[0].size - tmp;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100466#else
Matthew McClintock6fc7d3a2010-07-08 10:11:08 -0500467 return gd->bd->bi_memsize - tmp;
Marian Balakowicz2499b042008-03-12 12:14:15 +0100468#endif
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100469}
470
Grant Likely26396382011-03-28 09:58:43 +0000471phys_size_t getenv_bootm_mapsize(void)
472{
473 phys_size_t tmp;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000474 char *s = getenv("bootm_mapsize");
Grant Likely26396382011-03-28 09:58:43 +0000475 if (s) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000476 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
Grant Likely26396382011-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 Warren6904b6e2011-10-18 11:11:49 +0000487void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100488{
Larry Johnsone4551002010-04-20 08:09:43 -0400489 if (to == from)
490 return;
491
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100492#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
Sonic Zhang76ae5d92014-12-10 18:20:53 +0800493 if (to > from) {
494 from += len;
495 to += len;
496 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100497 while (len > 0) {
498 size_t tail = (len > chunksz) ? chunksz : len;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000499 WATCHDOG_RESET();
Sonic Zhang76ae5d92014-12-10 18:20:53 +0800500 if (to > from) {
501 to -= tail;
502 from -= tail;
503 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000504 memmove(to, from, tail);
Sonic Zhang76ae5d92014-12-10 18:20:53 +0800505 if (to < from) {
506 to += tail;
507 from += tail;
508 }
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100509 len -= tail;
510 }
511#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000512 memmove(to, from, len);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100513#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
514}
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100515#endif /* !USE_HOSTCC */
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100516
Stephen Warren6904b6e2011-10-18 11:11:49 +0000517void genimg_print_size(uint32_t size)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100518{
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100519#ifndef USE_HOSTCC
Stephen Warren6904b6e2011-10-18 11:11:49 +0000520 printf("%d Bytes = ", size);
521 print_size(size, "\n");
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100522#else
Stephen Warren6904b6e2011-10-18 11:11:49 +0000523 printf("%d Bytes = %.2f kB = %.2f MB\n",
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100524 size, (double)size / 1.024e3,
525 (double)size / 1.048576e6);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100526#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100527}
528
Simon Glassaf0ec0d2013-05-07 06:11:51 +0000529#if IMAGE_ENABLE_TIMESTAMP
530void genimg_print_time(time_t timestamp)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100531{
532#ifndef USE_HOSTCC
533 struct rtc_time tm;
534
Stephen Warren6904b6e2011-10-18 11:11:49 +0000535 to_tm(timestamp, &tm);
536 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
Marian Balakowiczc3c7eb22008-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 Warren6904b6e2011-10-18 11:11:49 +0000540 printf("%s", ctime(&timestamp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100541#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100542}
Simon Glassaf0ec0d2013-05-07 06:11:51 +0000543#endif
Marian Balakowiczc3c7eb22008-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 Frysingercf2feb12010-10-20 07:17:39 -0400559char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100560{
561 for (; table->id >= 0; ++table) {
562 if (table->id == id)
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200563#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
Scott Wood68498ab2009-04-02 16:15:10 -0500564 return table->lname;
565#else
566 return table->lname + gd->reloc_off;
567#endif
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100568 }
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100569 return (msg);
570}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100571
Stephen Warren6904b6e2011-10-18 11:11:49 +0000572const char *genimg_get_os_name(uint8_t os)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100573{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000574 return (get_table_entry_name(uimage_os, "Unknown OS", os));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100575}
576
Stephen Warren6904b6e2011-10-18 11:11:49 +0000577const char *genimg_get_arch_name(uint8_t arch)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100578{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000579 return (get_table_entry_name(uimage_arch, "Unknown Architecture",
580 arch));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100581}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100582
Stephen Warren6904b6e2011-10-18 11:11:49 +0000583const char *genimg_get_type_name(uint8_t type)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100584{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000585 return (get_table_entry_name(uimage_type, "Unknown Image", type));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100586}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100587
Stephen Warren6904b6e2011-10-18 11:11:49 +0000588const char *genimg_get_comp_name(uint8_t comp)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100589{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000590 return (get_table_entry_name(uimage_comp, "Unknown Compression",
591 comp));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100592}
593
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100594/**
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 Frysingercf2feb12010-10-20 07:17:39 -0400608int get_table_entry_id(const table_entry_t *table,
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100609 const char *table_name, const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100610{
Mike Frysingercf2feb12010-10-20 07:17:39 -0400611 const table_entry_t *t;
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100612#ifdef USE_HOSTCC
613 int first = 1;
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100614
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100615 for (t = table; t->id >= 0; ++t) {
616 if (t->sname && strcasecmp(t->sname, name) == 0)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000617 return(t->id);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100618 }
619
Stephen Warren6904b6e2011-10-18 11:11:49 +0000620 fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100621 for (t = table; t->id >= 0; ++t) {
622 if (t->sname == NULL)
623 continue;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000624 fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100625 first = 0;
626 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000627 fprintf(stderr, "\n");
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100628#else
629 for (t = table; t->id >= 0; ++t) {
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200630#ifdef CONFIG_NEEDS_MANUAL_RELOC
Scott Wood68498ab2009-04-02 16:15:10 -0500631 if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200632#else
633 if (t->sname && strcmp(t->sname, name) == 0)
Peter Tyser9057cbf2009-09-21 11:20:36 -0500634#endif
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100635 return (t->id);
636 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000637 debug("Invalid %s Type: %s\n", table_name, name);
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100638#endif /* USE_HOSTCC */
639 return (-1);
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100640}
641
Stephen Warren6904b6e2011-10-18 11:11:49 +0000642int genimg_get_os_id(const char *name)
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100643{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000644 return (get_table_entry_id(uimage_os, "OS", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100645}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100646
Stephen Warren6904b6e2011-10-18 11:11:49 +0000647int genimg_get_arch_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100648{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000649 return (get_table_entry_id(uimage_arch, "CPU", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100650}
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100651
Stephen Warren6904b6e2011-10-18 11:11:49 +0000652int genimg_get_type_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100653{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000654 return (get_table_entry_id(uimage_type, "Image", name));
Marian Balakowicz05c1d3f2008-01-31 13:20:07 +0100655}
Marian Balakowicz95418502008-01-31 13:55:39 +0100656
Stephen Warren6904b6e2011-10-18 11:11:49 +0000657int genimg_get_comp_id(const char *name)
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100658{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000659 return (get_table_entry_id(uimage_comp, "Compression", name));
Marian Balakowiczc3c7eb22008-02-29 15:59:59 +0100660}
661
662#ifndef USE_HOSTCC
Marian Balakowicz95418502008-01-31 13:55:39 +0100663/**
Bryan Wuf5c377a2014-08-15 16:51:38 -0700664 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
665 * FIT strings
Bryan Wuaf7b0de2014-07-31 17:39:58 -0700666 * @img_addr: a string might contain real image address
Bryan Wuf5c377a2014-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 Wuaf7b0de2014-07-31 17:39:58 -0700671 *
Bryan Wuf5c377a2014-08-15 16:51:38 -0700672 * genimg_get_kernel_addr_fit get the real kernel start address from a string
Bryan Wuaf7b0de2014-07-31 17:39:58 -0700673 * which is normally the first argv of bootm/bootz
674 *
675 * returns:
676 * kernel start address
677 */
Bryan Wuf5c377a2014-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 Wuaf7b0de2014-07-31 17:39:58 -0700681{
Bryan Wuaf7b0de2014-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 Wuf5c377a2014-08-15 16:51:38 -0700691 fit_uname_config)) {
Bryan Wuaf7b0de2014-07-31 17:39:58 -0700692 debug("* kernel: config '%s' from image at 0x%08lx\n",
Bryan Wuf5c377a2014-08-15 16:51:38 -0700693 *fit_uname_config, kernel_addr);
Bryan Wuaf7b0de2014-07-31 17:39:58 -0700694 } else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
Bryan Wuf5c377a2014-08-15 16:51:38 -0700695 fit_uname_kernel)) {
Bryan Wuaf7b0de2014-07-31 17:39:58 -0700696 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Bryan Wuf5c377a2014-08-15 16:51:38 -0700697 *fit_uname_kernel, kernel_addr);
Bryan Wuaf7b0de2014-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 Wuf5c377a2014-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100722 * genimg_get_format - get image format type
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100723 * @img_addr: image start address
724 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100725 * genimg_get_format() checks whether provided address points to a valid
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100726 * legacy or FIT image.
727 *
Marian Balakowicz12560682008-02-27 11:02:26 +0100728 * 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
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100730 * genimg_get_format() must be able to dectect libfdt header.
Marian Balakowicz12560682008-02-27 11:02:26 +0100731 *
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100732 * returns:
733 * image format type or IMAGE_FORMAT_INVALID if no image is present
734 */
Simon Glass49648062013-05-07 06:12:03 +0000735int genimg_get_format(const void *img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100736{
Heiko Schocher515eb122014-05-28 11:33:33 +0200737#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Wolfgang Denk3b506772009-08-19 11:42:56 +0200738 const image_header_t *hdr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100739
Wolfgang Denk3b506772009-08-19 11:42:56 +0200740 hdr = (const image_header_t *)img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100741 if (image_check_magic(hdr))
Heiko Schocher515eb122014-05-28 11:33:33 +0200742 return IMAGE_FORMAT_LEGACY;
743#endif
Marian Balakowicz12560682008-02-27 11:02:26 +0100744#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Heiko Schocher515eb122014-05-28 11:33:33 +0200745 if (fdt_check_header(img_addr) == 0)
746 return IMAGE_FORMAT_FIT;
Sebastian Siewior686d6672014-05-05 15:08:09 -0500747#endif
748#ifdef CONFIG_ANDROID_BOOT_IMAGE
Heiko Schocher515eb122014-05-28 11:33:33 +0200749 if (android_image_check_header(img_addr) == 0)
750 return IMAGE_FORMAT_ANDROID;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100751#endif
752
Heiko Schocher515eb122014-05-28 11:33:33 +0200753 return IMAGE_FORMAT_INVALID;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100754}
755
756/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100757 * genimg_get_image - get image from special storage (if necessary)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100758 * @img_addr: image start address
759 *
Guilherme Maciel Ferreirafc3c9672015-01-15 02:48:06 -0200760 * genimg_get_image() checks if provided image start address is located
Marian Balakowiczc536d6f2008-02-21 17:20:19 +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 Warren6904b6e2011-10-18 11:11:49 +0000766ulong genimg_get_image(ulong img_addr)
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100767{
Marian Balakowicz535b9102008-02-27 11:00:47 +0100768 ulong ram_addr = img_addr;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100769
770#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz535b9102008-02-27 11:00:47 +0100771 ulong h_size, d_size;
772
Stephen Warren6904b6e2011-10-18 11:11:49 +0000773 if (addr_dataflash(img_addr)) {
Simon Glass49648062013-05-07 06:12:03 +0000774 void *buf;
775
Marian Balakowicz535b9102008-02-27 11:00:47 +0100776 /* ger RAM address */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200777 ram_addr = CONFIG_SYS_LOAD_ADDR;
Marian Balakowicz535b9102008-02-27 11:00:47 +0100778
779 /* get header size */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000780 h_size = image_get_header_size();
Marian Balakowicz535b9102008-02-27 11:00:47 +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 Warren6904b6e2011-10-18 11:11:49 +0000787 debug(" Reading image header from dataflash address "
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100788 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100789
Simon Glass49648062013-05-07 06:12:03 +0000790 buf = map_sysmem(ram_addr, 0);
791 read_dataflash(img_addr, h_size, buf);
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100792
Marian Balakowicz535b9102008-02-27 11:00:47 +0100793 /* get data size */
Simon Glass49648062013-05-07 06:12:03 +0000794 switch (genimg_get_format(buf)) {
Heiko Schocher515eb122014-05-28 11:33:33 +0200795#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100796 case IMAGE_FORMAT_LEGACY:
Simon Glass49648062013-05-07 06:12:03 +0000797 d_size = image_get_data_size(buf);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000798 debug(" Legacy format image found at 0x%08lx, "
799 "size 0x%08lx\n",
Marian Balakowicz535b9102008-02-27 11:00:47 +0100800 ram_addr, d_size);
801 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200802#endif
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100803#if defined(CONFIG_FIT)
Marian Balakowicz535b9102008-02-27 11:00:47 +0100804 case IMAGE_FORMAT_FIT:
Simon Glass49648062013-05-07 06:12:03 +0000805 d_size = fit_get_size(buf) - h_size;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000806 debug(" FIT/FDT format image found at 0x%08lx, "
807 "size 0x%08lx\n",
Marian Balakowicz535b9102008-02-27 11:00:47 +0100808 ram_addr, d_size);
809 break;
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100810#endif
Marian Balakowicz535b9102008-02-27 11:00:47 +0100811 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000812 printf(" No valid image found at 0x%08lx\n",
813 img_addr);
Marian Balakowicz535b9102008-02-27 11:00:47 +0100814 return ram_addr;
815 }
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100816
Marian Balakowicz535b9102008-02-27 11:00:47 +0100817 /* read in image data */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000818 debug(" Reading image remaining data from dataflash address "
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100819 "%08lx to RAM address %08lx\n", img_addr + h_size,
820 ram_addr + h_size);
821
Stephen Warren6904b6e2011-10-18 11:11:49 +0000822 read_dataflash(img_addr + h_size, d_size,
Simon Glass49648062013-05-07 06:12:03 +0000823 (char *)(buf + h_size));
Marian Balakowicz535b9102008-02-27 11:00:47 +0100824
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100825 }
Marian Balakowicz535b9102008-02-27 11:00:47 +0100826#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100827
828 return ram_addr;
829}
830
831/**
Marian Balakowicz0cd4f3d2008-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 Warren6904b6e2011-10-18 11:11:49 +0000842int genimg_has_config(bootm_headers_t *images)
Marian Balakowicz0cd4f3d2008-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100852 * boot_get_ramdisk - main ramdisk handling routine
Marian Balakowicz95418502008-01-31 13:55:39 +0100853 * @argc: command argument count
854 * @argv: command argument list
Marian Balakowicz61fde552008-02-27 11:01:04 +0100855 * @images: pointer to the bootm images structure
Marian Balakowicz95418502008-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 Balakowiczd7c88a42008-02-29 14:58:34 +0100860 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Marian Balakowicz95418502008-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 Balakowicza0ffb422008-03-12 10:14:38 +0100866 * 0, if ramdisk image was found and valid, or skiped
Marian Balakowicz95418502008-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 Balakowicza0ffb422008-03-12 10:14:38 +0100869 *
Kumar Galab8ca7342008-08-29 19:08:29 -0500870 * 1, if ramdisk image is found but corrupted, or invalid
Marian Balakowicz95418502008-01-31 13:55:39 +0100871 * rd_start and rd_end are set to 0 if no ramdisk exists
Marian Balakowicz95418502008-01-31 13:55:39 +0100872 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000873int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100874 uint8_t arch, ulong *rd_start, ulong *rd_end)
Marian Balakowicz95418502008-01-31 13:55:39 +0100875{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100876 ulong rd_addr, rd_load;
Marian Balakowicz95418502008-01-31 13:55:39 +0100877 ulong rd_data, rd_len;
Heiko Schocher515eb122014-05-28 11:33:33 +0200878#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Wolfgang Denk3b506772009-08-19 11:42:56 +0200879 const image_header_t *rd_hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +0200880#endif
Simon Glass49648062013-05-07 06:12:03 +0000881 void *buf;
Marek Vasut6988d132012-03-30 23:19:10 +0200882#ifdef CONFIG_SUPPORT_RAW_INITRD
Marek Vasut28850d02012-03-18 11:47:58 +0000883 char *end;
Marek Vasut6988d132012-03-30 23:19:10 +0200884#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100885#if defined(CONFIG_FIT)
Simon Glassad68fc32013-07-10 23:08:10 -0700886 const char *fit_uname_config = images->fit_uname_cfg;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100887 const char *fit_uname_ramdisk = NULL;
888 ulong default_addr;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100889 int rd_noffset;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100890#endif
Simon Glass794a9212013-06-11 11:14:46 -0700891 const char *select = NULL;
Marian Balakowicz95418502008-01-31 13:55:39 +0100892
Marian Balakowicza50d5152008-03-12 10:12:37 +0100893 *rd_start = 0;
894 *rd_end = 0;
895
Simon Glass794a9212013-06-11 11:14:46 -0700896 if (argc >= 2)
897 select = argv[1];
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100898 /*
899 * Look for a '-' which indicates to ignore the
900 * ramdisk argument
901 */
Simon Glass794a9212013-06-11 11:14:46 -0700902 if (select && strcmp(select, "-") == 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000903 debug("## Skipping init Ramdisk\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100904 rd_len = rd_data = 0;
Simon Glass794a9212013-06-11 11:14:46 -0700905 } else if (select || genimg_has_config(images)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100906#if defined(CONFIG_FIT)
Simon Glass794a9212013-06-11 11:14:46 -0700907 if (select) {
Marian Balakowicz0cd4f3d2008-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 Balakowiczdbdd16a2008-02-04 08:28:09 +0100918
Simon Glass794a9212013-06-11 11:14:46 -0700919 if (fit_parse_conf(select, default_addr,
920 &rd_addr, &fit_uname_config)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000921 debug("* ramdisk: config '%s' from image at "
922 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100923 fit_uname_config, rd_addr);
Simon Glass794a9212013-06-11 11:14:46 -0700924 } else if (fit_parse_subimage(select, default_addr,
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100925 &rd_addr, &fit_uname_ramdisk)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000926 debug("* ramdisk: subimage '%s' from image at "
927 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100928 fit_uname_ramdisk, rd_addr);
929 } else
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100930#endif
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100931 {
Simon Glass794a9212013-06-11 11:14:46 -0700932 rd_addr = simple_strtoul(select, NULL, 16);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000933 debug("* ramdisk: cmdline image address = "
934 "0x%08lx\n",
Marian Balakowicz0cd4f3d2008-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 Glass65817a62013-05-16 13:53:22 +0000940 * command argument. If the property is not defined,
941 * quit silently.
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100942 */
Simon Glass49648062013-05-07 06:12:03 +0000943 rd_addr = map_to_sysmem(images->fit_hdr_os);
Simon Glass65817a62013-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 Tyser190ee482008-08-05 10:51:57 -0500947 return 0;
Simon Glass65817a62013-05-16 13:53:22 +0000948 else if (rd_noffset < 0)
949 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100950 }
Marian Balakowicz0cd4f3d2008-03-12 10:35:46 +0100951#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100952
953 /* copy from dataflash if needed */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000954 rd_addr = genimg_get_image(rd_addr);
Marian Balakowiczdbdd16a2008-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 Glass49648062013-05-07 06:12:03 +0000961 buf = map_sysmem(rd_addr, 0);
962 switch (genimg_get_format(buf)) {
Heiko Schocher515eb122014-05-28 11:33:33 +0200963#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100964 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000965 printf("## Loading init Ramdisk from Legacy "
Marian Balakowicza50d5152008-03-12 10:12:37 +0100966 "Image at %08lx ...\n", rd_addr);
Marian Balakowicz95418502008-01-31 13:55:39 +0100967
Simon Glass0169e6b2012-02-13 13:51:18 +0000968 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000969 rd_hdr = image_get_ramdisk(rd_addr, arch,
Marian Balakowicza0ffb422008-03-12 10:14:38 +0100970 images->verify);
Marian Balakowicz95418502008-01-31 13:55:39 +0100971
Marian Balakowicza50d5152008-03-12 10:12:37 +0100972 if (rd_hdr == NULL)
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600973 return 1;
Kumar Gala18f4c0f2008-02-27 21:51:46 -0600974
Stephen Warren6904b6e2011-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 Balakowiczdbdd16a2008-02-04 08:28:09 +0100978 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200979#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100980#if defined(CONFIG_FIT)
981 case IMAGE_FORMAT_FIT:
Simon Glassa0c0b632014-06-12 07:24:47 -0600982 rd_noffset = fit_image_load(images,
Simon Glass65817a62013-05-16 13:53:22 +0000983 rd_addr, &fit_uname_ramdisk,
Simon Glassad68fc32013-07-10 23:08:10 -0700984 &fit_uname_config, arch,
Simon Glass65817a62013-05-16 13:53:22 +0000985 IH_TYPE_RAMDISK,
986 BOOTSTAGE_ID_FIT_RD_START,
Simon Glass05a9ad72014-08-22 14:26:43 -0600987 FIT_LOAD_OPTIONAL_NON_ZERO,
988 &rd_data, &rd_len);
Simon Glass65817a62013-05-16 13:53:22 +0000989 if (rd_noffset < 0)
Michal Simekb2474002008-07-11 10:43:13 +0200990 return 1;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100991
Simon Glass65817a62013-05-16 13:53:22 +0000992 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
Marian Balakowicza50d5152008-03-12 10:12:37 +0100993 images->fit_uname_rd = fit_uname_ramdisk;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +0100994 images->fit_noffset_rd = rd_noffset;
Marian Balakowicza50d5152008-03-12 10:12:37 +0100995 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100996#endif
997 default:
Marek Vasut28850d02012-03-18 11:47:58 +0000998#ifdef CONFIG_SUPPORT_RAW_INITRD
Simon Glass794a9212013-06-11 11:14:46 -0700999 end = NULL;
1000 if (select)
1001 end = strchr(select, ':');
1002 if (end) {
Marek Vasut28850d02012-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 Balakowiczdbdd16a2008-02-04 08:28:09 +01001012 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001013 } else if (images->legacy_hdr_valid &&
Stephen Warren6904b6e2011-10-18 11:11:49 +00001014 image_check_type(&images->legacy_hdr_os_copy,
1015 IH_TYPE_MULTI)) {
1016
Marian Balakowicz95418502008-01-31 13:55:39 +01001017 /*
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001018 * Now check if we have a legacy mult-component image,
1019 * get second entry data start address and len.
Marian Balakowicz95418502008-01-31 13:55:39 +01001020 */
Simon Glass0169e6b2012-02-13 13:51:18 +00001021 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Stephen Warren6904b6e2011-10-18 11:11:49 +00001022 printf("## Loading init Ramdisk from multi component "
Marian Balakowicza50d5152008-03-12 10:12:37 +01001023 "Legacy Image at %08lx ...\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001024 (ulong)images->legacy_hdr_os);
1025
Stephen Warren6904b6e2011-10-18 11:11:49 +00001026 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
Sebastian Siewior686d6672014-05-05 15:08:09 -05001027 }
1028#ifdef CONFIG_ANDROID_BOOT_IMAGE
Ahmad Draidi9a1cb5d2014-10-23 20:50:07 +03001029 else if ((genimg_get_format((void *)images->os.start)
1030 == IMAGE_FORMAT_ANDROID) &&
Sebastian Siewior686d6672014-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 Balakowicz95418502008-01-31 13:55:39 +01001037 /*
1038 * no initrd image
1039 */
Simon Glass0169e6b2012-02-13 13:51:18 +00001040 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Marian Balakowicz95418502008-01-31 13:55:39 +01001041 rd_len = rd_data = 0;
1042 }
1043
1044 if (!rd_data) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001045 debug("## No init Ramdisk\n");
Marian Balakowicz95418502008-01-31 13:55:39 +01001046 } else {
1047 *rd_start = rd_data;
1048 *rd_end = rd_data + rd_len;
1049 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001050 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Marian Balakowicz95418502008-01-31 13:55:39 +01001051 *rd_start, *rd_end);
Kumar Gala18f4c0f2008-02-27 21:51:46 -06001052
1053 return 0;
Marian Balakowicz95418502008-01-31 13:55:39 +01001054}
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001055
John Rigbyeea8e692010-10-13 13:57:35 -06001056#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001057/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001058 * boot_ramdisk_high - relocate init ramdisk
Kumar Galab937bb72008-02-27 21:51:49 -06001059 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001060 * @rd_data: ramdisk data start address
1061 * @rd_len: ramdisk data length
Marian Balakowicz9c701e82008-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. Day832d36e2013-09-16 07:15:45 -04001067 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001068 * variable and if requested ramdisk data is moved to a specified location.
1069 *
Marian Balakowiczd7c88a42008-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 Balakowicz9c701e82008-01-31 13:57:17 +01001074 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001075 * 0 - success
1076 * -1 - failure
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001077 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001078int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Kumar Galab937bb72008-02-27 21:51:49 -06001079 ulong *initrd_start, ulong *initrd_end)
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001080{
1081 char *s;
1082 ulong initrd_high;
1083 int initrd_copy_to_ram = 1;
1084
Stephen Warren6904b6e2011-10-18 11:11:49 +00001085 if ((s = getenv("initrd_high")) != NULL) {
Marian Balakowicz9c701e82008-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 Warren6904b6e2011-10-18 11:11:49 +00001089 initrd_high = simple_strtoul(s, NULL, 16);
Marian Balakowicz9c701e82008-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 Balakowicz902ce702008-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 Warren6904b6e2011-10-18 11:11:49 +00001103 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Marian Balakowicz9c701e82008-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 Warren6904b6e2011-10-18 11:11:49 +00001108 debug(" in-place initrd\n");
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001109 *initrd_start = rd_data;
1110 *initrd_end = rd_data + rd_len;
Kumar Galab937bb72008-02-27 21:51:49 -06001111 lmb_reserve(lmb, rd_data, rd_len);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001112 } else {
Kumar Galab937bb72008-02-27 21:51:49 -06001113 if (initrd_high)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001114 *initrd_start = (ulong)lmb_alloc_base(lmb,
1115 rd_len, 0x1000, initrd_high);
Kumar Galab937bb72008-02-27 21:51:49 -06001116 else
Stephen Warren6904b6e2011-10-18 11:11:49 +00001117 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
1118 0x1000);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001119
Kumar Galab937bb72008-02-27 21:51:49 -06001120 if (*initrd_start == 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001121 puts("ramdisk - allocation error\n");
Kumar Galab937bb72008-02-27 21:51:49 -06001122 goto error;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001123 }
Simon Glass0169e6b2012-02-13 13:51:18 +00001124 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001125
1126 *initrd_end = *initrd_start + rd_len;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001127 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001128 *initrd_start, *initrd_end);
1129
Stephen Warren6904b6e2011-10-18 11:11:49 +00001130 memmove_wd((void *)*initrd_start,
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001131 (void *)rd_data, rd_len, CHUNKSZ);
1132
Kumar Gala180c5812011-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 Warren6904b6e2011-10-18 11:11:49 +00001141 puts("OK\n");
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001142 }
1143 } else {
1144 *initrd_start = 0;
1145 *initrd_end = 0;
1146 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001147 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001148 *initrd_start, *initrd_end);
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001149
Kumar Galab937bb72008-02-27 21:51:49 -06001150 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001151
Kumar Galab937bb72008-02-27 21:51:49 -06001152error:
1153 return -1;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001154}
John Rigbyeea8e692010-10-13 13:57:35 -06001155#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001156
Simon Glass0129b522014-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 Rigbyeea8e692010-10-13 13:57:35 -06001167#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001168/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001169 * boot_get_cmdline - allocate and initialize kernel cmdline
Kumar Galab937bb72008-02-27 21:51:49 -06001170 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-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 Balakowiczd7c88a42008-02-29 14:58:34 +01001174 * boot_get_cmdline() allocates space for kernel command line below
Grant Likelydfff7a22011-03-28 09:58:34 +00001175 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001176 * variable is present its contents is copied to allocated kernel
1177 * command line.
1178 *
1179 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001180 * 0 - success
1181 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001182 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001183int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001184{
1185 char *cmdline;
1186 char *s;
1187
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001188 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
Grant Likely26396382011-03-28 09:58:43 +00001189 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galab937bb72008-02-27 21:51:49 -06001190
1191 if (cmdline == NULL)
1192 return -1;
Marian Balakowicz2efc2642008-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 Warren6904b6e2011-10-18 11:11:49 +00001202 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001203
Kumar Galab937bb72008-02-27 21:51:49 -06001204 return 0;
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001205}
John Rigbyeea8e692010-10-13 13:57:35 -06001206#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001207
John Rigbyeea8e692010-10-13 13:57:35 -06001208#ifdef CONFIG_SYS_BOOT_GET_KBD
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001209/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001210 * boot_get_kbd - allocate and initialize kernel copy of board info
Kumar Galab937bb72008-02-27 21:51:49 -06001211 * @lmb: pointer to lmb handle, will be used for memory mgmt
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001212 * @kbd: double pointer to board info data
1213 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +01001214 * boot_get_kbd() allocates space for kernel copy of board info data below
Grant Likelydfff7a22011-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 Balakowicz2efc2642008-01-31 13:58:13 +01001217 *
1218 * returns:
Kumar Galab937bb72008-02-27 21:51:49 -06001219 * 0 - success
1220 * -1 - failure
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001221 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001222int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001223{
Becky Bruced26d67c2008-06-09 20:37:18 -05001224 *kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
Grant Likely26396382011-03-28 09:58:43 +00001225 getenv_bootm_mapsize() + getenv_bootm_low());
Kumar Galab937bb72008-02-27 21:51:49 -06001226 if (*kbd == NULL)
1227 return -1;
1228
Marian Balakowicz2efc2642008-01-31 13:58:13 +01001229 **kbd = *(gd->bd);
1230
Stephen Warren6904b6e2011-10-18 11:11:49 +00001231 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
Marian Balakowicz2efc2642008-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 Galab937bb72008-02-27 21:51:49 -06001237 return 0;
Marian Balakowicz9c701e82008-01-31 13:57:17 +01001238}
John Rigbyeea8e692010-10-13 13:57:35 -06001239#endif /* CONFIG_SYS_BOOT_GET_KBD */
Simon Glass9ca37422013-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 Balakowicze227fbb2008-02-29 21:24:06 +01001286#endif /* !USE_HOSTCC */