Blame view

common/image.c 33.2 KB
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
1
2
3
4
5
6
  /*
   * (C) Copyright 2008 Semihalf
   *
   * (C) Copyright 2000-2006
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
7
   * SPDX-License-Identifier:	GPL-2.0+
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
8
   */
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
9

b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
10
  #ifndef USE_HOSTCC
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
11
12
13
14
15
16
17
18
19
20
  #include <common.h>
  #include <watchdog.h>
  
  #ifdef CONFIG_SHOW_BOOT_PROGRESS
  #include <status_led.h>
  #endif
  
  #ifdef CONFIG_HAS_DATAFLASH
  #include <dataflash.h>
  #endif
95d449ad4   Marian Balakowicz   Avoid initrd and ...
21
22
23
  #ifdef CONFIG_LOGBUFFER
  #include <logbuff.h>
  #endif
2242f5369   Marian Balakowicz   [new uImage] Rena...
24
  #include <rtc.h>
2242f5369   Marian Balakowicz   [new uImage] Rena...
25

1cf0a8b2f   Joe Hershberger   env: Add a loadad...
26
  #include <environment.h>
5dfb52138   Marian Balakowicz   [new uImage] New ...
27
  #include <image.h>
712fbcf38   Stephen Warren   checkpatch whites...
28
  #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
fff888a19   Marian Balakowicz   [new uImage] Add ...
29
30
  #include <libfdt.h>
  #include <fdt_support.h>
c87796483   Marian Balakowicz   [new uImage] Add ...
31
  #endif
20a14a42a   Andy Fleming   Rename include/md...
32
  #include <u-boot/md5.h>
2b9912e6a   Jeroen Hofstee   includes: move op...
33
  #include <u-boot/sha1.h>
a51ec63b8   Simon Glass   image: Use fit_im...
34
  #include <asm/errno.h>
35e7b0f17   Simon Glass   sandbox: image: A...
35
  #include <asm/io.h>
c87796483   Marian Balakowicz   [new uImage] Add ...
36

b6b0fe646   Marian Balakowicz   [new uImage] Clea...
37
  #ifdef CONFIG_CMD_BDI
54841ab50   Wolfgang Denk   Make sure that ar...
38
  extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
39
40
41
  #endif
  
  DECLARE_GLOBAL_DATA_PTR;
8a5ea3e61   Marian Balakowicz   [new uImage] Move...
42

21d29f7f9   Heiko Schocher   bootm: make use o...
43
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
712fbcf38   Stephen Warren   checkpatch whites...
44
  static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
d985c8498   Marian Balakowicz   [new uImage] Remo...
45
  						int verify);
21d29f7f9   Heiko Schocher   bootm: make use o...
46
  #endif
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
47
  #else
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
48
  #include "mkimage.h"
20a14a42a   Andy Fleming   Rename include/md...
49
  #include <u-boot/md5.h>
5dfb52138   Marian Balakowicz   [new uImage] New ...
50
  #include <time.h>
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
51
  #include <image.h>
5dfb52138   Marian Balakowicz   [new uImage] New ...
52
  #endif /* !USE_HOSTCC*/
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
53

0ccff500c   Simon Glass   image: Use crc he...
54
  #include <u-boot/crc.h>
13d06981a   Simon Glass   image: Add device...
55
56
57
  #ifndef CONFIG_SYS_BARGSIZE
  #define CONFIG_SYS_BARGSIZE 512
  #endif
7edb186fc   Mike Frysinger   image: constify l...
58
  static const table_entry_t uimage_arch[] = {
570abb0ad   Marian Balakowicz   [new uImage] Shar...
59
60
61
62
63
64
65
66
67
  	{	IH_ARCH_INVALID,	NULL,		"Invalid ARCH",	},
  	{	IH_ARCH_ALPHA,		"alpha",	"Alpha",	},
  	{	IH_ARCH_ARM,		"arm",		"ARM",		},
  	{	IH_ARCH_I386,		"x86",		"Intel x86",	},
  	{	IH_ARCH_IA64,		"ia64",		"IA64",		},
  	{	IH_ARCH_M68K,		"m68k",		"M68K",		},
  	{	IH_ARCH_MICROBLAZE,	"microblaze",	"MicroBlaze",	},
  	{	IH_ARCH_MIPS,		"mips",		"MIPS",		},
  	{	IH_ARCH_MIPS64,		"mips64",	"MIPS 64 Bit",	},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
68
  	{	IH_ARCH_NIOS2,		"nios2",	"NIOS II",	},
e419e12d0   Grant Erickson   Recognize 'powerp...
69
  	{	IH_ARCH_PPC,		"powerpc",	"PowerPC",	},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
70
71
72
73
74
75
76
  	{	IH_ARCH_PPC,		"ppc",		"PowerPC",	},
  	{	IH_ARCH_S390,		"s390",		"IBM S390",	},
  	{	IH_ARCH_SH,		"sh",		"SuperH",	},
  	{	IH_ARCH_SPARC,		"sparc",	"SPARC",	},
  	{	IH_ARCH_SPARC64,	"sparc64",	"SPARC 64 Bit",	},
  	{	IH_ARCH_BLACKFIN,	"blackfin",	"Blackfin",	},
  	{	IH_ARCH_AVR32,		"avr32",	"AVR32",	},
64d614617   Macpaul Lin   nds32: common bdi...
77
  	{	IH_ARCH_NDS32,		"nds32",	"NDS32",	},
3ddcaccda   Stefan Kristiansson   openrisc: Add arc...
78
  	{	IH_ARCH_OPENRISC,	"or1k",		"OpenRISC 1000",},
35e7b0f17   Simon Glass   sandbox: image: A...
79
  	{	IH_ARCH_SANDBOX,	"sandbox",	"Sandbox",	},
0ae765312   David Feng   arm64: core support
80
  	{	IH_ARCH_ARM64,		"arm64",	"AArch64",	},
bc5d54288   Alexey Brodkin   arc: bdinfo, imag...
81
  	{	IH_ARCH_ARC,		"arc",		"ARC",		},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
82
83
  	{	-1,			"",		"",		},
  };
7edb186fc   Mike Frysinger   image: constify l...
84
  static const table_entry_t uimage_os[] = {
570abb0ad   Marian Balakowicz   [new uImage] Shar...
85
  	{	IH_OS_INVALID,	NULL,		"Invalid OS",		},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
86
87
88
89
90
  	{	IH_OS_LINUX,	"linux",	"Linux",		},
  #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
  	{	IH_OS_LYNXOS,	"lynxos",	"LynxOS",		},
  #endif
  	{	IH_OS_NETBSD,	"netbsd",	"NetBSD",		},
3df619579   Torkel Lundgren   Add support for o...
91
  	{	IH_OS_OSE,	"ose",		"Enea OSE",		},
04d414090   Steven Stallion   image: Add suppor...
92
  	{	IH_OS_PLAN9,	"plan9",	"Plan 9",		},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
93
94
  	{	IH_OS_RTEMS,	"rtems",	"RTEMS",		},
  	{	IH_OS_U_BOOT,	"u-boot",	"U-Boot",		},
68b15e831   miao.yan@windriver.com   common/image.c: m...
95
  	{	IH_OS_VXWORKS,	"vxworks",	"VxWorks",		},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
96
97
  #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
  	{	IH_OS_QNX,	"qnx",		"QNX",			},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
98
  #endif
f5ed9e390   Peter Tyser   Add support for b...
99
100
101
  #if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
  	{	IH_OS_INTEGRITY,"integrity",	"INTEGRITY",		},
  #endif
570abb0ad   Marian Balakowicz   [new uImage] Shar...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  #ifdef USE_HOSTCC
  	{	IH_OS_4_4BSD,	"4_4bsd",	"4_4BSD",		},
  	{	IH_OS_DELL,	"dell",		"Dell",			},
  	{	IH_OS_ESIX,	"esix",		"Esix",			},
  	{	IH_OS_FREEBSD,	"freebsd",	"FreeBSD",		},
  	{	IH_OS_IRIX,	"irix",		"Irix",			},
  	{	IH_OS_NCR,	"ncr",		"NCR",			},
  	{	IH_OS_OPENBSD,	"openbsd",	"OpenBSD",		},
  	{	IH_OS_PSOS,	"psos",		"pSOS",			},
  	{	IH_OS_SCO,	"sco",		"SCO",			},
  	{	IH_OS_SOLARIS,	"solaris",	"Solaris",		},
  	{	IH_OS_SVR4,	"svr4",		"SVR4",			},
  #endif
  	{	-1,		"",		"",			},
  };
7edb186fc   Mike Frysinger   image: constify l...
117
  static const table_entry_t uimage_type[] = {
4962e38e9   Stefano Babic   mkimage: adding s...
118
  	{	IH_TYPE_AISIMAGE,   "aisimage",   "Davinci AIS image",},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
119
120
  	{	IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",	},
  	{	IH_TYPE_FIRMWARE,   "firmware",	  "Firmware",		},
3decb14ab   John Rigby   mkimage: Add OMAP...
121
  	{	IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",	},
bf411ea9f   Karicheri, Muralidharan   tools: mkimage: a...
122
  	{	IH_TYPE_GPIMAGE,    "gpimage",    "TI Keystone SPL Image",},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
123
  	{	IH_TYPE_KERNEL,	    "kernel",	  "Kernel Image",	},
b9b50e89d   Stephen Warren   image: Implement ...
124
  	{	IH_TYPE_KERNEL_NOLOAD, "kernel_noload",  "Kernel Image (no loading done)", },
4962e38e9   Stefano Babic   mkimage: adding s...
125
126
127
  	{	IH_TYPE_KWBIMAGE,   "kwbimage",   "Kirkwood Boot Image",},
  	{	IH_TYPE_IMXIMAGE,   "imximage",   "Freescale i.MX Boot Image",},
  	{	IH_TYPE_INVALID,    NULL,	  "Invalid Image",	},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
128
  	{	IH_TYPE_MULTI,	    "multi",	  "Multi-File Image",	},
4962e38e9   Stefano Babic   mkimage: adding s...
129
  	{	IH_TYPE_OMAPIMAGE,  "omapimage",  "TI OMAP SPL With GP CH",},
5d898a00f   Shaohui Xie   powerpc/CoreNet: ...
130
  	{	IH_TYPE_PBLIMAGE,   "pblimage",   "Freescale PBL Boot Image",},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
131
132
  	{	IH_TYPE_RAMDISK,    "ramdisk",	  "RAMDisk Image",	},
  	{	IH_TYPE_SCRIPT,     "script",	  "Script",		},
832472a94   Charles Manning   tools: socfpga: A...
133
  	{	IH_TYPE_SOCFPGAIMAGE, "socfpgaimage", "Altera SOCFPGA preloader",},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
134
  	{	IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
7816f2cf8   Heiko Schocher   mkimage: add UBL ...
135
  	{	IH_TYPE_UBLIMAGE,   "ublimage",   "Davinci UBL image",},
bce883707   Marek Vasut   ARM: mxs: tools: ...
136
  	{	IH_TYPE_MXSIMAGE,   "mxsimage",   "Freescale MXS Boot Image",},
7b1a41174   Andreas Bießmann   mkimage: add atme...
137
  	{	IH_TYPE_ATMELIMAGE, "atmelimage", "ATMEL ROM-Boot Image",},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
138
139
  	{	-1,		    "",		  "",			},
  };
7edb186fc   Mike Frysinger   image: constify l...
140
  static const table_entry_t uimage_comp[] = {
570abb0ad   Marian Balakowicz   [new uImage] Shar...
141
142
143
  	{	IH_COMP_NONE,	"none",		"uncompressed",		},
  	{	IH_COMP_BZIP2,	"bzip2",	"bzip2 compressed",	},
  	{	IH_COMP_GZIP,	"gzip",		"gzip compressed",	},
fc9c1727b   Luigi 'Comio' Mantellini   Add support for L...
144
  	{	IH_COMP_LZMA,	"lzma",		"lzma compressed",	},
20dde48bc   Peter Korsgaard   add lzop decompre...
145
  	{	IH_COMP_LZO,	"lzo",		"lzo compressed",	},
570abb0ad   Marian Balakowicz   [new uImage] Shar...
146
147
  	{	-1,		"",		"",			},
  };
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
148
149
150
  /*****************************************************************************/
  /* Legacy format routines */
  /*****************************************************************************/
712fbcf38   Stephen Warren   checkpatch whites...
151
  int image_check_hcrc(const image_header_t *hdr)
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
152
153
  {
  	ulong hcrc;
712fbcf38   Stephen Warren   checkpatch whites...
154
  	ulong len = image_get_header_size();
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
155
156
157
  	image_header_t header;
  
  	/* Copy header so we can blank CRC field for re-calculation */
712fbcf38   Stephen Warren   checkpatch whites...
158
159
  	memmove(&header, (char *)hdr, image_get_header_size());
  	image_set_hcrc(&header, 0);
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
160

712fbcf38   Stephen Warren   checkpatch whites...
161
  	hcrc = crc32(0, (unsigned char *)&header, len);
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
162

712fbcf38   Stephen Warren   checkpatch whites...
163
  	return (hcrc == image_get_hcrc(hdr));
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
164
  }
712fbcf38   Stephen Warren   checkpatch whites...
165
  int image_check_dcrc(const image_header_t *hdr)
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
166
  {
712fbcf38   Stephen Warren   checkpatch whites...
167
168
169
  	ulong data = image_get_data(hdr);
  	ulong len = image_get_data_size(hdr);
  	ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
170

712fbcf38   Stephen Warren   checkpatch whites...
171
  	return (dcrc == image_get_dcrc(hdr));
b97a2a0a2   Marian Balakowicz   [new uImage] Defi...
172
  }
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
173
174
175
176
177
178
179
180
181
182
183
184
185
  /**
   * image_multi_count - get component (sub-image) count
   * @hdr: pointer to the header of the multi component image
   *
   * image_multi_count() returns number of components in a multi
   * component image.
   *
   * Note: no checking of the image type is done, caller must pass
   * a valid multi component image.
   *
   * returns:
   *     number of components
   */
712fbcf38   Stephen Warren   checkpatch whites...
186
  ulong image_multi_count(const image_header_t *hdr)
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
187
188
  {
  	ulong i, count = 0;
df6f1b895   Marian Balakowicz   [new uImage] Fix ...
189
  	uint32_t *size;
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
190
191
192
  
  	/* get start of the image payload, which in case of multi
  	 * component images that points to a table of component sizes */
712fbcf38   Stephen Warren   checkpatch whites...
193
  	size = (uint32_t *)image_get_data(hdr);
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  
  	/* count non empty slots */
  	for (i = 0; size[i]; ++i)
  		count++;
  
  	return count;
  }
  
  /**
   * image_multi_getimg - get component data address and size
   * @hdr: pointer to the header of the multi component image
   * @idx: index of the requested component
   * @data: pointer to a ulong variable, will hold component data address
   * @len: pointer to a ulong variable, will hold component size
   *
   * image_multi_getimg() returns size and data address for the requested
   * component in a multi component image.
   *
   * Note: no checking of the image type is done, caller must pass
   * a valid multi component image.
   *
   * returns:
   *     data address and size of the component, if idx is valid
   *     0 in data and len, if idx is out of range
   */
712fbcf38   Stephen Warren   checkpatch whites...
219
  void image_multi_getimg(const image_header_t *hdr, ulong idx,
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
220
221
222
  			ulong *data, ulong *len)
  {
  	int i;
df6f1b895   Marian Balakowicz   [new uImage] Fix ...
223
  	uint32_t *size;
02b9b2244   Nick Spence   Fix offset calcul...
224
  	ulong offset, count, img_data;
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
225
226
  
  	/* get number of component */
712fbcf38   Stephen Warren   checkpatch whites...
227
  	count = image_multi_count(hdr);
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
228
229
230
  
  	/* get start of the image payload, which in case of multi
  	 * component images that points to a table of component sizes */
712fbcf38   Stephen Warren   checkpatch whites...
231
  	size = (uint32_t *)image_get_data(hdr);
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
232
233
234
  
  	/* get address of the proper component data start, which means
  	 * skipping sizes table (add 1 for last, null entry) */
712fbcf38   Stephen Warren   checkpatch whites...
235
  	img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
236
237
  
  	if (idx < count) {
712fbcf38   Stephen Warren   checkpatch whites...
238
  		*len = uimage_to_cpu(size[idx]);
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
239
  		offset = 0;
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
240
241
242
  
  		/* go over all indices preceding requested component idx */
  		for (i = 0; i < idx; i++) {
02b9b2244   Nick Spence   Fix offset calcul...
243
  			/* add up i-th component size, rounding up to 4 bytes */
712fbcf38   Stephen Warren   checkpatch whites...
244
  			offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
245
246
247
  		}
  
  		/* calculate idx-th component data address */
02b9b2244   Nick Spence   Fix offset calcul...
248
  		*data = img_data + offset;
f13e7b2e9   Marian Balakowicz   [new uImage] Clea...
249
250
251
252
253
  	} else {
  		*len = 0;
  		*data = 0;
  	}
  }
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
254

712fbcf38   Stephen Warren   checkpatch whites...
255
  static void image_print_type(const image_header_t *hdr)
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
256
257
  {
  	const char *os, *arch, *type, *comp;
712fbcf38   Stephen Warren   checkpatch whites...
258
259
260
261
  	os = genimg_get_os_name(image_get_os(hdr));
  	arch = genimg_get_arch_name(image_get_arch(hdr));
  	type = genimg_get_type_name(image_get_type(hdr));
  	comp = genimg_get_comp_name(image_get_comp(hdr));
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
262

712fbcf38   Stephen Warren   checkpatch whites...
263
264
  	printf("%s %s %s (%s)
  ", arch, os, type, comp);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
265
  }
5dfb52138   Marian Balakowicz   [new uImage] New ...
266
  /**
edbed247a   Bartlomiej Sieka   Memory footprint ...
267
   * image_print_contents - prints out the contents of the legacy format image
3a2003f61   Wolfgang Denk   tools/mkimage: fi...
268
   * @ptr: pointer to the legacy format image header
5dfb52138   Marian Balakowicz   [new uImage] New ...
269
270
   * @p: pointer to prefix string
   *
edbed247a   Bartlomiej Sieka   Memory footprint ...
271
   * image_print_contents() formats a multi line legacy image contents description.
5dfb52138   Marian Balakowicz   [new uImage] New ...
272
273
274
275
276
277
   * The routine prints out all header fields followed by the size/offset data
   * for MULTI/SCRIPT images.
   *
   * returns:
   *     no returned results
   */
712fbcf38   Stephen Warren   checkpatch whites...
278
  void image_print_contents(const void *ptr)
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
279
  {
3a2003f61   Wolfgang Denk   tools/mkimage: fi...
280
  	const image_header_t *hdr = (const image_header_t *)ptr;
edbed247a   Bartlomiej Sieka   Memory footprint ...
281
  	const char *p;
1fe7d9389   Simon Glass   image: Remove rem...
282
  	p = IMAGE_INDENT_STRING;
712fbcf38   Stephen Warren   checkpatch whites...
283
284
  	printf("%sImage Name:   %.*s
  ", p, IH_NMLEN, image_get_name(hdr));
859e92b77   Simon Glass   image: Move times...
285
286
287
288
  	if (IMAGE_ENABLE_TIMESTAMP) {
  		printf("%sCreated:      ", p);
  		genimg_print_time((time_t)image_get_time(hdr));
  	}
712fbcf38   Stephen Warren   checkpatch whites...
289
290
291
292
293
294
295
296
297
298
299
  	printf("%sImage Type:   ", p);
  	image_print_type(hdr);
  	printf("%sData Size:    ", p);
  	genimg_print_size(image_get_data_size(hdr));
  	printf("%sLoad Address: %08x
  ", p, image_get_load(hdr));
  	printf("%sEntry Point:  %08x
  ", p, image_get_ep(hdr));
  
  	if (image_check_type(hdr, IH_TYPE_MULTI) ||
  			image_check_type(hdr, IH_TYPE_SCRIPT)) {
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
300
301
  		int i;
  		ulong data, len;
712fbcf38   Stephen Warren   checkpatch whites...
302
  		ulong count = image_multi_count(hdr);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
303

712fbcf38   Stephen Warren   checkpatch whites...
304
305
  		printf("%sContents:
  ", p);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
306
  		for (i = 0; i < count; i++) {
712fbcf38   Stephen Warren   checkpatch whites...
307
  			image_multi_getimg(hdr, i, &data, &len);
570abb0ad   Marian Balakowicz   [new uImage] Shar...
308

712fbcf38   Stephen Warren   checkpatch whites...
309
310
  			printf("%s   Image %d: ", p, i);
  			genimg_print_size(len);
570abb0ad   Marian Balakowicz   [new uImage] Shar...
311

712fbcf38   Stephen Warren   checkpatch whites...
312
  			if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
570abb0ad   Marian Balakowicz   [new uImage] Shar...
313
314
315
316
317
  				/*
  				 * the user may need to know offsets
  				 * if planning to do something with
  				 * multiple files
  				 */
712fbcf38   Stephen Warren   checkpatch whites...
318
319
  				printf("%s    Offset = 0x%08lx
  ", p, data);
570abb0ad   Marian Balakowicz   [new uImage] Shar...
320
  			}
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
321
322
323
  		}
  	}
  }
570abb0ad   Marian Balakowicz   [new uImage] Shar...
324
325
  
  #ifndef USE_HOSTCC
21d29f7f9   Heiko Schocher   bootm: make use o...
326
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
327
328
  /**
   * image_get_ramdisk - get and verify ramdisk image
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
   * @rd_addr: ramdisk image start address
   * @arch: expected ramdisk architecture
   * @verify: checksum verification flag
   *
   * image_get_ramdisk() returns a pointer to the verified ramdisk image
   * header. Routine receives image start address and expected architecture
   * flag. Verification done covers data and header integrity and os/type/arch
   * fields checking.
   *
   * If dataflash support is enabled routine checks for dataflash addresses
   * and handles required dataflash reads.
   *
   * returns:
   *     pointer to a ramdisk image header, if image was found and valid
   *     otherwise, return NULL
   */
712fbcf38   Stephen Warren   checkpatch whites...
345
  static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
d985c8498   Marian Balakowicz   [new uImage] Remo...
346
  						int verify)
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
347
  {
3a2003f61   Wolfgang Denk   tools/mkimage: fi...
348
  	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
349

712fbcf38   Stephen Warren   checkpatch whites...
350
351
352
  	if (!image_check_magic(rd_hdr)) {
  		puts("Bad Magic Number
  ");
770605e4f   Simon Glass   bootstage: Replac...
353
  		bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
354
355
  		return NULL;
  	}
712fbcf38   Stephen Warren   checkpatch whites...
356
357
358
  	if (!image_check_hcrc(rd_hdr)) {
  		puts("Bad Header Checksum
  ");
770605e4f   Simon Glass   bootstage: Replac...
359
  		bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
360
361
  		return NULL;
  	}
770605e4f   Simon Glass   bootstage: Replac...
362
  	bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
712fbcf38   Stephen Warren   checkpatch whites...
363
  	image_print_contents(rd_hdr);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
364
365
366
  
  	if (verify) {
  		puts("   Verifying Checksum ... ");
712fbcf38   Stephen Warren   checkpatch whites...
367
368
369
  		if (!image_check_dcrc(rd_hdr)) {
  			puts("Bad Data CRC
  ");
770605e4f   Simon Glass   bootstage: Replac...
370
  			bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
371
372
373
374
375
  			return NULL;
  		}
  		puts("OK
  ");
  	}
770605e4f   Simon Glass   bootstage: Replac...
376
  	bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
377

712fbcf38   Stephen Warren   checkpatch whites...
378
379
380
381
382
  	if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
  	    !image_check_arch(rd_hdr, arch) ||
  	    !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
  		printf("No Linux %s Ramdisk Image
  ",
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
383
  				genimg_get_arch_name(arch));
770605e4f   Simon Glass   bootstage: Replac...
384
  		bootstage_error(BOOTSTAGE_ID_RAMDISK);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
385
386
387
388
389
  		return NULL;
  	}
  
  	return rd_hdr;
  }
21d29f7f9   Heiko Schocher   bootm: make use o...
390
  #endif
570abb0ad   Marian Balakowicz   [new uImage] Shar...
391
  #endif /* !USE_HOSTCC */
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
392
393
394
395
  
  /*****************************************************************************/
  /* Shared dual-format routines */
  /*****************************************************************************/
570abb0ad   Marian Balakowicz   [new uImage] Shar...
396
  #ifndef USE_HOSTCC
1cf0a8b2f   Joe Hershberger   env: Add a loadad...
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
  ulong load_addr = CONFIG_SYS_LOAD_ADDR;	/* Default Load Address */
  ulong save_addr;			/* Default Save Address */
  ulong save_size;			/* Default Save Size (in bytes) */
  
  static int on_loadaddr(const char *name, const char *value, enum env_op op,
  	int flags)
  {
  	switch (op) {
  	case env_op_create:
  	case env_op_overwrite:
  		load_addr = simple_strtoul(value, NULL, 16);
  		break;
  	default:
  		break;
  	}
  
  	return 0;
  }
  U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
416
417
  ulong getenv_bootm_low(void)
  {
712fbcf38   Stephen Warren   checkpatch whites...
418
  	char *s = getenv("bootm_low");
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
419
  	if (s) {
712fbcf38   Stephen Warren   checkpatch whites...
420
  		ulong tmp = simple_strtoul(s, NULL, 16);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
421
422
  		return tmp;
  	}
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
423
424
  #if defined(CONFIG_SYS_SDRAM_BASE)
  	return CONFIG_SYS_SDRAM_BASE;
afe45c87e   Marian Balakowicz   [new uImage] Fix ...
425
426
  #elif defined(CONFIG_ARM)
  	return gd->bd->bi_dram[0].start;
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
427
428
429
430
  #else
  	return 0;
  #endif
  }
391fd93ab   Becky Bruce   Change lmb to use...
431
  phys_size_t getenv_bootm_size(void)
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
432
  {
c519facc6   Matthew McClintock   Fix condition whe...
433
  	phys_size_t tmp;
712fbcf38   Stephen Warren   checkpatch whites...
434
  	char *s = getenv("bootm_size");
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
435
  	if (s) {
712fbcf38   Stephen Warren   checkpatch whites...
436
  		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
437
438
  		return tmp;
  	}
c519facc6   Matthew McClintock   Fix condition whe...
439
440
  	s = getenv("bootm_low");
  	if (s)
712fbcf38   Stephen Warren   checkpatch whites...
441
  		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
c519facc6   Matthew McClintock   Fix condition whe...
442
443
  	else
  		tmp = 0;
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
444

afe45c87e   Marian Balakowicz   [new uImage] Fix ...
445
  #if defined(CONFIG_ARM)
c519facc6   Matthew McClintock   Fix condition whe...
446
  	return gd->bd->bi_dram[0].size - tmp;
afe45c87e   Marian Balakowicz   [new uImage] Fix ...
447
  #else
c519facc6   Matthew McClintock   Fix condition whe...
448
  	return gd->bd->bi_memsize - tmp;
afe45c87e   Marian Balakowicz   [new uImage] Fix ...
449
  #endif
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
450
  }
c3624e6ed   Grant Likely   Default to bootm_...
451
452
453
  phys_size_t getenv_bootm_mapsize(void)
  {
  	phys_size_t tmp;
712fbcf38   Stephen Warren   checkpatch whites...
454
  	char *s = getenv("bootm_mapsize");
c3624e6ed   Grant Likely   Default to bootm_...
455
  	if (s) {
712fbcf38   Stephen Warren   checkpatch whites...
456
  		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
c3624e6ed   Grant Likely   Default to bootm_...
457
458
459
460
461
462
463
464
465
  		return tmp;
  	}
  
  #if defined(CONFIG_SYS_BOOTMAPSZ)
  	return CONFIG_SYS_BOOTMAPSZ;
  #else
  	return getenv_bootm_size();
  #endif
  }
712fbcf38   Stephen Warren   checkpatch whites...
466
  void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
467
  {
54fa2c5b5   Larry Johnson   Move test for unn...
468
469
  	if (to == from)
  		return;
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
470
471
472
  #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  	while (len > 0) {
  		size_t tail = (len > chunksz) ? chunksz : len;
712fbcf38   Stephen Warren   checkpatch whites...
473
474
  		WATCHDOG_RESET();
  		memmove(to, from, tail);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
475
476
477
478
479
  		to += tail;
  		from += tail;
  		len -= tail;
  	}
  #else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
712fbcf38   Stephen Warren   checkpatch whites...
480
  	memmove(to, from, len);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
481
482
  #endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  }
570abb0ad   Marian Balakowicz   [new uImage] Shar...
483
  #endif /* !USE_HOSTCC */
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
484

712fbcf38   Stephen Warren   checkpatch whites...
485
  void genimg_print_size(uint32_t size)
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
486
  {
570abb0ad   Marian Balakowicz   [new uImage] Shar...
487
  #ifndef USE_HOSTCC
712fbcf38   Stephen Warren   checkpatch whites...
488
489
490
  	printf("%d Bytes = ", size);
  	print_size(size, "
  ");
570abb0ad   Marian Balakowicz   [new uImage] Shar...
491
  #else
712fbcf38   Stephen Warren   checkpatch whites...
492
493
  	printf("%d Bytes = %.2f kB = %.2f MB
  ",
570abb0ad   Marian Balakowicz   [new uImage] Shar...
494
495
  			size, (double)size / 1.024e3,
  			(double)size / 1.048576e6);
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
496
  #endif
570abb0ad   Marian Balakowicz   [new uImage] Shar...
497
  }
859e92b77   Simon Glass   image: Move times...
498
499
  #if IMAGE_ENABLE_TIMESTAMP
  void genimg_print_time(time_t timestamp)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
500
501
502
  {
  #ifndef USE_HOSTCC
  	struct rtc_time tm;
712fbcf38   Stephen Warren   checkpatch whites...
503
504
505
  	to_tm(timestamp, &tm);
  	printf("%4d-%02d-%02d  %2d:%02d:%02d UTC
  ",
570abb0ad   Marian Balakowicz   [new uImage] Shar...
506
507
508
  			tm.tm_year, tm.tm_mon, tm.tm_mday,
  			tm.tm_hour, tm.tm_min, tm.tm_sec);
  #else
712fbcf38   Stephen Warren   checkpatch whites...
509
  	printf("%s", ctime(&timestamp));
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
510
  #endif
570abb0ad   Marian Balakowicz   [new uImage] Shar...
511
  }
859e92b77   Simon Glass   image: Move times...
512
  #endif
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
513

570abb0ad   Marian Balakowicz   [new uImage] Shar...
514
515
516
517
518
519
520
521
522
523
524
525
526
527
  /**
   * get_table_entry_name - translate entry id to long name
   * @table: pointer to a translation table for entries of a specific type
   * @msg: message to be returned when translation fails
   * @id: entry id to be translated
   *
   * get_table_entry_name() will go over translation table trying to find
   * entry that matches given id. If matching entry is found, its long
   * name is returned to the caller.
   *
   * returns:
   *     long entry name if translation succeeds
   *     msg otherwise
   */
7edb186fc   Mike Frysinger   image: constify l...
528
  char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
529
530
531
  {
  	for (; table->id >= 0; ++table) {
  		if (table->id == id)
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
532
  #if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
e3d1ac7bb   Scott Wood   common/image.c: R...
533
534
535
536
  			return table->lname;
  #else
  			return table->lname + gd->reloc_off;
  #endif
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
537
  	}
570abb0ad   Marian Balakowicz   [new uImage] Shar...
538
539
  	return (msg);
  }
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
540

712fbcf38   Stephen Warren   checkpatch whites...
541
  const char *genimg_get_os_name(uint8_t os)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
542
  {
712fbcf38   Stephen Warren   checkpatch whites...
543
  	return (get_table_entry_name(uimage_os, "Unknown OS", os));
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
544
  }
712fbcf38   Stephen Warren   checkpatch whites...
545
  const char *genimg_get_arch_name(uint8_t arch)
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
546
  {
712fbcf38   Stephen Warren   checkpatch whites...
547
548
  	return (get_table_entry_name(uimage_arch, "Unknown Architecture",
  					arch));
570abb0ad   Marian Balakowicz   [new uImage] Shar...
549
  }
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
550

712fbcf38   Stephen Warren   checkpatch whites...
551
  const char *genimg_get_type_name(uint8_t type)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
552
  {
712fbcf38   Stephen Warren   checkpatch whites...
553
  	return (get_table_entry_name(uimage_type, "Unknown Image", type));
570abb0ad   Marian Balakowicz   [new uImage] Shar...
554
  }
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
555

712fbcf38   Stephen Warren   checkpatch whites...
556
  const char *genimg_get_comp_name(uint8_t comp)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
557
  {
712fbcf38   Stephen Warren   checkpatch whites...
558
559
  	return (get_table_entry_name(uimage_comp, "Unknown Compression",
  					comp));
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
560
  }
570abb0ad   Marian Balakowicz   [new uImage] Shar...
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  /**
   * get_table_entry_id - translate short entry name to id
   * @table: pointer to a translation table for entries of a specific type
   * @table_name: to be used in case of error
   * @name: entry short name to be translated
   *
   * get_table_entry_id() will go over translation table trying to find
   * entry that matches given short name. If matching entry is found,
   * its id returned to the caller.
   *
   * returns:
   *     entry id if translation succeeds
   *     -1 otherwise
   */
7edb186fc   Mike Frysinger   image: constify l...
575
  int get_table_entry_id(const table_entry_t *table,
570abb0ad   Marian Balakowicz   [new uImage] Shar...
576
  		const char *table_name, const char *name)
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
577
  {
7edb186fc   Mike Frysinger   image: constify l...
578
  	const table_entry_t *t;
570abb0ad   Marian Balakowicz   [new uImage] Shar...
579
580
  #ifdef USE_HOSTCC
  	int first = 1;
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
581

570abb0ad   Marian Balakowicz   [new uImage] Shar...
582
583
  	for (t = table; t->id >= 0; ++t) {
  		if (t->sname && strcasecmp(t->sname, name) == 0)
712fbcf38   Stephen Warren   checkpatch whites...
584
  			return(t->id);
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
585
  	}
712fbcf38   Stephen Warren   checkpatch whites...
586
587
  	fprintf(stderr, "
  Invalid %s Type - valid names are", table_name);
570abb0ad   Marian Balakowicz   [new uImage] Shar...
588
589
590
  	for (t = table; t->id >= 0; ++t) {
  		if (t->sname == NULL)
  			continue;
712fbcf38   Stephen Warren   checkpatch whites...
591
  		fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
570abb0ad   Marian Balakowicz   [new uImage] Shar...
592
593
  		first = 0;
  	}
712fbcf38   Stephen Warren   checkpatch whites...
594
595
  	fprintf(stderr, "
  ");
570abb0ad   Marian Balakowicz   [new uImage] Shar...
596
597
  #else
  	for (t = table; t->id >= 0; ++t) {
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
598
  #ifdef CONFIG_NEEDS_MANUAL_RELOC
e3d1ac7bb   Scott Wood   common/image.c: R...
599
  		if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
2e5167cca   Wolfgang Denk   Replace CONFIG_RE...
600
601
  #else
  		if (t->sname && strcmp(t->sname, name) == 0)
521af04d8   Peter Tyser   Conditionally per...
602
  #endif
570abb0ad   Marian Balakowicz   [new uImage] Shar...
603
604
  			return (t->id);
  	}
712fbcf38   Stephen Warren   checkpatch whites...
605
606
  	debug("Invalid %s Type: %s
  ", table_name, name);
570abb0ad   Marian Balakowicz   [new uImage] Shar...
607
608
609
  #endif /* USE_HOSTCC */
  	return (-1);
  }
712fbcf38   Stephen Warren   checkpatch whites...
610
  int genimg_get_os_id(const char *name)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
611
  {
712fbcf38   Stephen Warren   checkpatch whites...
612
  	return (get_table_entry_id(uimage_os, "OS", name));
570abb0ad   Marian Balakowicz   [new uImage] Shar...
613
  }
712fbcf38   Stephen Warren   checkpatch whites...
614
  int genimg_get_arch_id(const char *name)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
615
  {
712fbcf38   Stephen Warren   checkpatch whites...
616
  	return (get_table_entry_id(uimage_arch, "CPU", name));
42b73e8ee   Marian Balakowicz   [new uImage] Fact...
617
  }
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
618

712fbcf38   Stephen Warren   checkpatch whites...
619
  int genimg_get_type_id(const char *name)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
620
  {
712fbcf38   Stephen Warren   checkpatch whites...
621
  	return (get_table_entry_id(uimage_type, "Image", name));
570abb0ad   Marian Balakowicz   [new uImage] Shar...
622
  }
712fbcf38   Stephen Warren   checkpatch whites...
623
  int genimg_get_comp_id(const char *name)
570abb0ad   Marian Balakowicz   [new uImage] Shar...
624
  {
712fbcf38   Stephen Warren   checkpatch whites...
625
  	return (get_table_entry_id(uimage_comp, "Compression", name));
570abb0ad   Marian Balakowicz   [new uImage] Shar...
626
627
628
  }
  
  #ifndef USE_HOSTCC
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
629
  /**
6c454fedf   Bryan Wu   image: fix bootm ...
630
631
   * genimg_get_kernel_addr_fit - get the real kernel address and return 2
   *                              FIT strings
0f64140b6   Bryan Wu   image: introduce ...
632
   * @img_addr: a string might contain real image address
6c454fedf   Bryan Wu   image: fix bootm ...
633
634
635
636
   * @fit_uname_config: double pointer to a char, will hold pointer to a
   *                    configuration unit name
   * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
   *                    name
0f64140b6   Bryan Wu   image: introduce ...
637
   *
6c454fedf   Bryan Wu   image: fix bootm ...
638
   * genimg_get_kernel_addr_fit get the real kernel start address from a string
0f64140b6   Bryan Wu   image: introduce ...
639
640
641
642
643
   * which is normally the first argv of bootm/bootz
   *
   * returns:
   *     kernel start address
   */
6c454fedf   Bryan Wu   image: fix bootm ...
644
645
646
  ulong genimg_get_kernel_addr_fit(char * const img_addr,
  			     const char **fit_uname_config,
  			     const char **fit_uname_kernel)
0f64140b6   Bryan Wu   image: introduce ...
647
  {
0f64140b6   Bryan Wu   image: introduce ...
648
649
650
651
652
653
654
655
656
657
  	ulong kernel_addr;
  
  	/* find out kernel image address */
  	if (!img_addr) {
  		kernel_addr = load_addr;
  		debug("*  kernel: default image load address = 0x%08lx
  ",
  		      load_addr);
  #if defined(CONFIG_FIT)
  	} else if (fit_parse_conf(img_addr, load_addr, &kernel_addr,
6c454fedf   Bryan Wu   image: fix bootm ...
658
  				  fit_uname_config)) {
0f64140b6   Bryan Wu   image: introduce ...
659
660
  		debug("*  kernel: config '%s' from image at 0x%08lx
  ",
6c454fedf   Bryan Wu   image: fix bootm ...
661
  		      *fit_uname_config, kernel_addr);
0f64140b6   Bryan Wu   image: introduce ...
662
  	} else if (fit_parse_subimage(img_addr, load_addr, &kernel_addr,
6c454fedf   Bryan Wu   image: fix bootm ...
663
  				     fit_uname_kernel)) {
0f64140b6   Bryan Wu   image: introduce ...
664
665
  		debug("*  kernel: subimage '%s' from image at 0x%08lx
  ",
6c454fedf   Bryan Wu   image: fix bootm ...
666
  		      *fit_uname_kernel, kernel_addr);
0f64140b6   Bryan Wu   image: introduce ...
667
668
669
670
671
672
673
674
675
676
677
678
  #endif
  	} else {
  		kernel_addr = simple_strtoul(img_addr, NULL, 16);
  		debug("*  kernel: cmdline image address = 0x%08lx
  ",
  		      kernel_addr);
  	}
  
  	return kernel_addr;
  }
  
  /**
6c454fedf   Bryan Wu   image: fix bootm ...
679
680
681
682
683
684
685
686
687
688
689
690
691
   * genimg_get_kernel_addr() is the simple version of
   * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
   */
  ulong genimg_get_kernel_addr(char * const img_addr)
  {
  	const char *fit_uname_config = NULL;
  	const char *fit_uname_kernel = NULL;
  
  	return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
  					  &fit_uname_kernel);
  }
  
  /**
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
692
   * genimg_get_format - get image format type
fff888a19   Marian Balakowicz   [new uImage] Add ...
693
694
   * @img_addr: image start address
   *
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
695
   * genimg_get_format() checks whether provided address points to a valid
fff888a19   Marian Balakowicz   [new uImage] Add ...
696
697
   * legacy or FIT image.
   *
4efbe9dbb   Marian Balakowicz   [new uImage] Corr...
698
699
   * New uImage format and FDT blob are based on a libfdt. FDT blob
   * may be passed directly or embedded in a FIT image. In both situations
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
700
   * genimg_get_format() must be able to dectect libfdt header.
4efbe9dbb   Marian Balakowicz   [new uImage] Corr...
701
   *
fff888a19   Marian Balakowicz   [new uImage] Add ...
702
703
704
   * returns:
   *     image format type or IMAGE_FORMAT_INVALID if no image is present
   */
35e7b0f17   Simon Glass   sandbox: image: A...
705
  int genimg_get_format(const void *img_addr)
fff888a19   Marian Balakowicz   [new uImage] Add ...
706
  {
21d29f7f9   Heiko Schocher   bootm: make use o...
707
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
3a2003f61   Wolfgang Denk   tools/mkimage: fi...
708
  	const image_header_t *hdr;
fff888a19   Marian Balakowicz   [new uImage] Add ...
709

3a2003f61   Wolfgang Denk   tools/mkimage: fi...
710
  	hdr = (const image_header_t *)img_addr;
fff888a19   Marian Balakowicz   [new uImage] Add ...
711
  	if (image_check_magic(hdr))
21d29f7f9   Heiko Schocher   bootm: make use o...
712
713
  		return IMAGE_FORMAT_LEGACY;
  #endif
4efbe9dbb   Marian Balakowicz   [new uImage] Corr...
714
  #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
21d29f7f9   Heiko Schocher   bootm: make use o...
715
716
  	if (fdt_check_header(img_addr) == 0)
  		return IMAGE_FORMAT_FIT;
9ace3fc81   Sebastian Siewior   image: add suppor...
717
718
  #endif
  #ifdef CONFIG_ANDROID_BOOT_IMAGE
21d29f7f9   Heiko Schocher   bootm: make use o...
719
720
  	if (android_image_check_header(img_addr) == 0)
  		return IMAGE_FORMAT_ANDROID;
fff888a19   Marian Balakowicz   [new uImage] Add ...
721
  #endif
21d29f7f9   Heiko Schocher   bootm: make use o...
722
  	return IMAGE_FORMAT_INVALID;
fff888a19   Marian Balakowicz   [new uImage] Add ...
723
724
725
  }
  
  /**
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
726
   * genimg_get_image - get image from special storage (if necessary)
fff888a19   Marian Balakowicz   [new uImage] Add ...
727
728
   * @img_addr: image start address
   *
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
729
   * genimg_get_image() checks if provided image start adddress is located
fff888a19   Marian Balakowicz   [new uImage] Add ...
730
731
732
733
734
   * in a dataflash storage. If so, image is moved to a system RAM memory.
   *
   * returns:
   *     image start address after possible relocation from special storage
   */
712fbcf38   Stephen Warren   checkpatch whites...
735
  ulong genimg_get_image(ulong img_addr)
fff888a19   Marian Balakowicz   [new uImage] Add ...
736
  {
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
737
  	ulong ram_addr = img_addr;
fff888a19   Marian Balakowicz   [new uImage] Add ...
738
739
  
  #ifdef CONFIG_HAS_DATAFLASH
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
740
  	ulong h_size, d_size;
712fbcf38   Stephen Warren   checkpatch whites...
741
  	if (addr_dataflash(img_addr)) {
35e7b0f17   Simon Glass   sandbox: image: A...
742
  		void *buf;
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
743
  		/* ger RAM address */
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
744
  		ram_addr = CONFIG_SYS_LOAD_ADDR;
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
745
746
  
  		/* get header size */
712fbcf38   Stephen Warren   checkpatch whites...
747
  		h_size = image_get_header_size();
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
748
749
750
751
752
753
  #if defined(CONFIG_FIT)
  		if (sizeof(struct fdt_header) > h_size)
  			h_size = sizeof(struct fdt_header);
  #endif
  
  		/* read in header */
712fbcf38   Stephen Warren   checkpatch whites...
754
  		debug("   Reading image header from dataflash address "
fff888a19   Marian Balakowicz   [new uImage] Add ...
755
756
  			"%08lx to RAM address %08lx
  ", img_addr, ram_addr);
fff888a19   Marian Balakowicz   [new uImage] Add ...
757

35e7b0f17   Simon Glass   sandbox: image: A...
758
759
  		buf = map_sysmem(ram_addr, 0);
  		read_dataflash(img_addr, h_size, buf);
fff888a19   Marian Balakowicz   [new uImage] Add ...
760

6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
761
  		/* get data size */
35e7b0f17   Simon Glass   sandbox: image: A...
762
  		switch (genimg_get_format(buf)) {
21d29f7f9   Heiko Schocher   bootm: make use o...
763
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
764
  		case IMAGE_FORMAT_LEGACY:
35e7b0f17   Simon Glass   sandbox: image: A...
765
  			d_size = image_get_data_size(buf);
712fbcf38   Stephen Warren   checkpatch whites...
766
767
768
  			debug("   Legacy format image found at 0x%08lx, "
  					"size 0x%08lx
  ",
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
769
770
  					ram_addr, d_size);
  			break;
21d29f7f9   Heiko Schocher   bootm: make use o...
771
  #endif
fff888a19   Marian Balakowicz   [new uImage] Add ...
772
  #if defined(CONFIG_FIT)
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
773
  		case IMAGE_FORMAT_FIT:
35e7b0f17   Simon Glass   sandbox: image: A...
774
  			d_size = fit_get_size(buf) - h_size;
712fbcf38   Stephen Warren   checkpatch whites...
775
776
777
  			debug("   FIT/FDT format image found at 0x%08lx, "
  					"size 0x%08lx
  ",
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
778
779
  					ram_addr, d_size);
  			break;
fff888a19   Marian Balakowicz   [new uImage] Add ...
780
  #endif
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
781
  		default:
712fbcf38   Stephen Warren   checkpatch whites...
782
783
784
  			printf("   No valid image found at 0x%08lx
  ",
  				img_addr);
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
785
786
  			return ram_addr;
  		}
fff888a19   Marian Balakowicz   [new uImage] Add ...
787

6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
788
  		/* read in image data */
712fbcf38   Stephen Warren   checkpatch whites...
789
  		debug("   Reading image remaining data from dataflash address "
fff888a19   Marian Balakowicz   [new uImage] Add ...
790
791
792
  			"%08lx to RAM address %08lx
  ", img_addr + h_size,
  			ram_addr + h_size);
712fbcf38   Stephen Warren   checkpatch whites...
793
  		read_dataflash(img_addr + h_size, d_size,
35e7b0f17   Simon Glass   sandbox: image: A...
794
  				(char *)(buf + h_size));
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
795

fff888a19   Marian Balakowicz   [new uImage] Add ...
796
  	}
6f0f9dfc4   Marian Balakowicz   [new uImage] Opti...
797
  #endif /* CONFIG_HAS_DATAFLASH */
fff888a19   Marian Balakowicz   [new uImage] Add ...
798
799
800
801
802
  
  	return ram_addr;
  }
  
  /**
f773bea8e   Marian Balakowicz   [new uImage] Add ...
803
804
805
806
807
808
809
810
811
812
   * fit_has_config - check if there is a valid FIT configuration
   * @images: pointer to the bootm command headers structure
   *
   * fit_has_config() checks if there is a FIT configuration in use
   * (if FTI support is present).
   *
   * returns:
   *     0, no FIT support or no configuration found
   *     1, configuration found
   */
712fbcf38   Stephen Warren   checkpatch whites...
813
  int genimg_has_config(bootm_headers_t *images)
f773bea8e   Marian Balakowicz   [new uImage] Add ...
814
815
816
817
818
819
820
821
822
  {
  #if defined(CONFIG_FIT)
  	if (images->fit_uname_cfg)
  		return 1;
  #endif
  	return 0;
  }
  
  /**
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
823
   * boot_get_ramdisk - main ramdisk handling routine
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
824
825
   * @argc: command argument count
   * @argv: command argument list
8a5ea3e61   Marian Balakowicz   [new uImage] Move...
826
   * @images: pointer to the bootm images structure
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
827
828
829
830
   * @arch: expected ramdisk architecture
   * @rd_start: pointer to a ulong variable, will hold ramdisk start address
   * @rd_end: pointer to a ulong variable, will hold ramdisk end
   *
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
831
   * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
832
833
834
835
836
   * Curently supported are the following ramdisk sources:
   *      - multicomponent kernel/ramdisk image,
   *      - commandline provided address of decicated ramdisk image.
   *
   * returns:
d985c8498   Marian Balakowicz   [new uImage] Remo...
837
   *     0, if ramdisk image was found and valid, or skiped
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
838
839
   *     rd_start and rd_end are set to ramdisk start/end addresses if
   *     ramdisk image is found and valid
d985c8498   Marian Balakowicz   [new uImage] Remo...
840
   *
ea86b9e64   Kumar Gala   Prevent crash if ...
841
   *     1, if ramdisk image is found but corrupted, or invalid
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
842
   *     rd_start and rd_end are set to 0 if no ramdisk exists
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
843
   */
712fbcf38   Stephen Warren   checkpatch whites...
844
  int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
d985c8498   Marian Balakowicz   [new uImage] Remo...
845
  		uint8_t arch, ulong *rd_start, ulong *rd_end)
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
846
  {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
847
  	ulong rd_addr, rd_load;
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
848
  	ulong rd_data, rd_len;
21d29f7f9   Heiko Schocher   bootm: make use o...
849
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
3a2003f61   Wolfgang Denk   tools/mkimage: fi...
850
  	const image_header_t *rd_hdr;
21d29f7f9   Heiko Schocher   bootm: make use o...
851
  #endif
35e7b0f17   Simon Glass   sandbox: image: A...
852
  	void *buf;
57d40ab70   Marek Vasut   RAMDISK: Fix unus...
853
  #ifdef CONFIG_SUPPORT_RAW_INITRD
017e1f3f9   Marek Vasut   BOOT: Add RAW ram...
854
  	char *end;
57d40ab70   Marek Vasut   RAMDISK: Fix unus...
855
  #endif
d5934ad77   Marian Balakowicz   [new uImage] Add ...
856
  #if defined(CONFIG_FIT)
f320a4d84   Simon Glass   bootm: Use select...
857
  	const char	*fit_uname_config = images->fit_uname_cfg;
d5934ad77   Marian Balakowicz   [new uImage] Add ...
858
859
  	const char	*fit_uname_ramdisk = NULL;
  	ulong		default_addr;
c87796483   Marian Balakowicz   [new uImage] Add ...
860
  	int		rd_noffset;
d5934ad77   Marian Balakowicz   [new uImage] Add ...
861
  #endif
983c72f47   Simon Glass   Clarify bootm OS ...
862
  	const char *select = NULL;
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
863

c87796483   Marian Balakowicz   [new uImage] Add ...
864
865
  	*rd_start = 0;
  	*rd_end = 0;
983c72f47   Simon Glass   Clarify bootm OS ...
866
867
  	if (argc >= 2)
  		select = argv[1];
d5934ad77   Marian Balakowicz   [new uImage] Add ...
868
869
870
871
  	/*
  	 * Look for a '-' which indicates to ignore the
  	 * ramdisk argument
  	 */
983c72f47   Simon Glass   Clarify bootm OS ...
872
  	if (select && strcmp(select, "-") ==  0) {
712fbcf38   Stephen Warren   checkpatch whites...
873
874
  		debug("## Skipping init Ramdisk
  ");
d5934ad77   Marian Balakowicz   [new uImage] Add ...
875
  		rd_len = rd_data = 0;
983c72f47   Simon Glass   Clarify bootm OS ...
876
  	} else if (select || genimg_has_config(images)) {
d5934ad77   Marian Balakowicz   [new uImage] Add ...
877
  #if defined(CONFIG_FIT)
983c72f47   Simon Glass   Clarify bootm OS ...
878
  		if (select) {
f773bea8e   Marian Balakowicz   [new uImage] Add ...
879
880
881
882
883
884
885
886
887
888
  			/*
  			 * If the init ramdisk comes from the FIT image and
  			 * the FIT image address is omitted in the command
  			 * line argument, try to use os FIT image address or
  			 * default load address.
  			 */
  			if (images->fit_uname_os)
  				default_addr = (ulong)images->fit_hdr_os;
  			else
  				default_addr = load_addr;
983c72f47   Simon Glass   Clarify bootm OS ...
889
890
  			if (fit_parse_conf(select, default_addr,
  					   &rd_addr, &fit_uname_config)) {
712fbcf38   Stephen Warren   checkpatch whites...
891
892
893
  				debug("*  ramdisk: config '%s' from image at "
  						"0x%08lx
  ",
f773bea8e   Marian Balakowicz   [new uImage] Add ...
894
  						fit_uname_config, rd_addr);
983c72f47   Simon Glass   Clarify bootm OS ...
895
  			} else if (fit_parse_subimage(select, default_addr,
f773bea8e   Marian Balakowicz   [new uImage] Add ...
896
  						&rd_addr, &fit_uname_ramdisk)) {
712fbcf38   Stephen Warren   checkpatch whites...
897
898
899
  				debug("*  ramdisk: subimage '%s' from image at "
  						"0x%08lx
  ",
f773bea8e   Marian Balakowicz   [new uImage] Add ...
900
901
  						fit_uname_ramdisk, rd_addr);
  			} else
d5934ad77   Marian Balakowicz   [new uImage] Add ...
902
  #endif
f773bea8e   Marian Balakowicz   [new uImage] Add ...
903
  			{
983c72f47   Simon Glass   Clarify bootm OS ...
904
  				rd_addr = simple_strtoul(select, NULL, 16);
712fbcf38   Stephen Warren   checkpatch whites...
905
906
907
  				debug("*  ramdisk: cmdline image address = "
  						"0x%08lx
  ",
f773bea8e   Marian Balakowicz   [new uImage] Add ...
908
909
910
911
912
  						rd_addr);
  			}
  #if defined(CONFIG_FIT)
  		} else {
  			/* use FIT configuration provided in first bootm
a51ec63b8   Simon Glass   image: Use fit_im...
913
914
  			 * command argument. If the property is not defined,
  			 * quit silently.
f773bea8e   Marian Balakowicz   [new uImage] Add ...
915
  			 */
35e7b0f17   Simon Glass   sandbox: image: A...
916
  			rd_addr = map_to_sysmem(images->fit_hdr_os);
a51ec63b8   Simon Glass   image: Use fit_im...
917
918
919
  			rd_noffset = fit_get_node_from_config(images,
  					FIT_RAMDISK_PROP, rd_addr);
  			if (rd_noffset == -ENOLINK)
41266c9b5   Peter Tyser   FIT: Fix handling...
920
  				return 0;
a51ec63b8   Simon Glass   image: Use fit_im...
921
922
  			else if (rd_noffset < 0)
  				return 1;
d5934ad77   Marian Balakowicz   [new uImage] Add ...
923
  		}
f773bea8e   Marian Balakowicz   [new uImage] Add ...
924
  #endif
d5934ad77   Marian Balakowicz   [new uImage] Add ...
925
926
  
  		/* copy from dataflash if needed */
712fbcf38   Stephen Warren   checkpatch whites...
927
  		rd_addr = genimg_get_image(rd_addr);
d5934ad77   Marian Balakowicz   [new uImage] Add ...
928
929
930
931
932
933
  
  		/*
  		 * Check if there is an initrd image at the
  		 * address provided in the second bootm argument
  		 * check image type, for FIT images get FIT node.
  		 */
35e7b0f17   Simon Glass   sandbox: image: A...
934
935
  		buf = map_sysmem(rd_addr, 0);
  		switch (genimg_get_format(buf)) {
21d29f7f9   Heiko Schocher   bootm: make use o...
936
  #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad77   Marian Balakowicz   [new uImage] Add ...
937
  		case IMAGE_FORMAT_LEGACY:
712fbcf38   Stephen Warren   checkpatch whites...
938
  			printf("## Loading init Ramdisk from Legacy "
c87796483   Marian Balakowicz   [new uImage] Add ...
939
940
  					"Image at %08lx ...
  ", rd_addr);
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
941

770605e4f   Simon Glass   bootstage: Replac...
942
  			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
712fbcf38   Stephen Warren   checkpatch whites...
943
  			rd_hdr = image_get_ramdisk(rd_addr, arch,
d985c8498   Marian Balakowicz   [new uImage] Remo...
944
  							images->verify);
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
945

c87796483   Marian Balakowicz   [new uImage] Add ...
946
  			if (rd_hdr == NULL)
274cea2bd   Kumar Gala   [new uImage] rewo...
947
  				return 1;
274cea2bd   Kumar Gala   [new uImage] rewo...
948

712fbcf38   Stephen Warren   checkpatch whites...
949
950
951
  			rd_data = image_get_data(rd_hdr);
  			rd_len = image_get_data_size(rd_hdr);
  			rd_load = image_get_load(rd_hdr);
d5934ad77   Marian Balakowicz   [new uImage] Add ...
952
  			break;
21d29f7f9   Heiko Schocher   bootm: make use o...
953
  #endif
d5934ad77   Marian Balakowicz   [new uImage] Add ...
954
955
  #if defined(CONFIG_FIT)
  		case IMAGE_FORMAT_FIT:
126cc8642   Simon Glass   image: Remove the...
956
  			rd_noffset = fit_image_load(images,
a51ec63b8   Simon Glass   image: Use fit_im...
957
  					rd_addr, &fit_uname_ramdisk,
f320a4d84   Simon Glass   bootm: Use select...
958
  					&fit_uname_config, arch,
a51ec63b8   Simon Glass   image: Use fit_im...
959
960
  					IH_TYPE_RAMDISK,
  					BOOTSTAGE_ID_FIT_RD_START,
fe20a81a6   Simon Glass   Fix test failure ...
961
962
  					FIT_LOAD_OPTIONAL_NON_ZERO,
  					&rd_data, &rd_len);
a51ec63b8   Simon Glass   image: Use fit_im...
963
  			if (rd_noffset < 0)
c78fce699   Michal Simek   FIS: repare incor...
964
  				return 1;
c87796483   Marian Balakowicz   [new uImage] Add ...
965

a51ec63b8   Simon Glass   image: Use fit_im...
966
  			images->fit_hdr_rd = map_sysmem(rd_addr, 0);
c87796483   Marian Balakowicz   [new uImage] Add ...
967
  			images->fit_uname_rd = fit_uname_ramdisk;
3dfe11014   Marian Balakowicz   [new uImage] Add ...
968
  			images->fit_noffset_rd = rd_noffset;
c87796483   Marian Balakowicz   [new uImage] Add ...
969
  			break;
d5934ad77   Marian Balakowicz   [new uImage] Add ...
970
971
  #endif
  		default:
017e1f3f9   Marek Vasut   BOOT: Add RAW ram...
972
  #ifdef CONFIG_SUPPORT_RAW_INITRD
983c72f47   Simon Glass   Clarify bootm OS ...
973
974
975
976
  			end = NULL;
  			if (select)
  				end = strchr(select, ':');
  			if (end) {
017e1f3f9   Marek Vasut   BOOT: Add RAW ram...
977
978
979
980
981
982
983
984
985
986
  				rd_len = simple_strtoul(++end, NULL, 16);
  				rd_data = rd_addr;
  			} else
  #endif
  			{
  				puts("Wrong Ramdisk Image Format
  ");
  				rd_data = rd_len = rd_load = 0;
  				return 1;
  			}
d5934ad77   Marian Balakowicz   [new uImage] Add ...
987
  		}
d5934ad77   Marian Balakowicz   [new uImage] Add ...
988
  	} else if (images->legacy_hdr_valid &&
712fbcf38   Stephen Warren   checkpatch whites...
989
990
  			image_check_type(&images->legacy_hdr_os_copy,
  						IH_TYPE_MULTI)) {
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
991
  		/*
d5934ad77   Marian Balakowicz   [new uImage] Add ...
992
993
  		 * Now check if we have a legacy mult-component image,
  		 * get second entry data start address and len.
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
994
  		 */
770605e4f   Simon Glass   bootstage: Replac...
995
  		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
712fbcf38   Stephen Warren   checkpatch whites...
996
  		printf("## Loading init Ramdisk from multi component "
c87796483   Marian Balakowicz   [new uImage] Add ...
997
998
  				"Legacy Image at %08lx ...
  ",
d5934ad77   Marian Balakowicz   [new uImage] Add ...
999
  				(ulong)images->legacy_hdr_os);
712fbcf38   Stephen Warren   checkpatch whites...
1000
  		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
9ace3fc81   Sebastian Siewior   image: add suppor...
1001
1002
1003
1004
1005
1006
1007
1008
1009
  	}
  #ifdef CONFIG_ANDROID_BOOT_IMAGE
  	else if ((genimg_get_format(images) == IMAGE_FORMAT_ANDROID) &&
  		 (!android_image_get_ramdisk((void *)images->os.start,
  		 &rd_data, &rd_len))) {
  		/* empty */
  	}
  #endif
  	else {
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
1010
1011
1012
  		/*
  		 * no initrd image
  		 */
770605e4f   Simon Glass   bootstage: Replac...
1013
  		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
1014
1015
1016
1017
  		rd_len = rd_data = 0;
  	}
  
  	if (!rd_data) {
712fbcf38   Stephen Warren   checkpatch whites...
1018
1019
  		debug("## No init Ramdisk
  ");
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
1020
1021
1022
1023
  	} else {
  		*rd_start = rd_data;
  		*rd_end = rd_data + rd_len;
  	}
712fbcf38   Stephen Warren   checkpatch whites...
1024
1025
  	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx
  ",
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
1026
  			*rd_start, *rd_end);
274cea2bd   Kumar Gala   [new uImage] rewo...
1027
1028
  
  	return 0;
5ad03eb38   Marian Balakowicz   [new uImage] Fact...
1029
  }
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1030

fca43cc80   John Rigby   boot: change some...
1031
  #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1032
  /**
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
1033
   * boot_ramdisk_high - relocate init ramdisk
e822d7fc4   Kumar Gala   [new uImage] Use ...
1034
   * @lmb: pointer to lmb handle, will be used for memory mgmt
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1035
1036
   * @rd_data: ramdisk data start address
   * @rd_len: ramdisk data length
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1037
1038
1039
1040
1041
   * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
   *      start address (after possible relocation)
   * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
   *      end address (after possible relocation)
   *
1bce2aeb6   Robert P. J. Day   Cosmetic: Fix a n...
1042
   * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1043
1044
   * variable and if requested ramdisk data is moved to a specified location.
   *
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
1045
1046
1047
1048
   * Initrd_start and initrd_end are set to final (after relocation) ramdisk
   * start/end addresses if ramdisk image start and len were provided,
   * otherwise set initrd_start and initrd_end set to zeros.
   *
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1049
   * returns:
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
1050
1051
   *      0 - success
   *     -1 - failure
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1052
   */
712fbcf38   Stephen Warren   checkpatch whites...
1053
  int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
e822d7fc4   Kumar Gala   [new uImage] Use ...
1054
  		  ulong *initrd_start, ulong *initrd_end)
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1055
1056
1057
1058
  {
  	char	*s;
  	ulong	initrd_high;
  	int	initrd_copy_to_ram = 1;
712fbcf38   Stephen Warren   checkpatch whites...
1059
  	if ((s = getenv("initrd_high")) != NULL) {
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1060
1061
1062
  		/* a value of "no" or a similar string will act like 0,
  		 * turning the "load high" feature off. This is intentional.
  		 */
712fbcf38   Stephen Warren   checkpatch whites...
1063
  		initrd_high = simple_strtoul(s, NULL, 16);
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1064
1065
1066
1067
1068
1069
  		if (initrd_high == ~0)
  			initrd_copy_to_ram = 0;
  	} else {
  		/* not set, no restrictions to load high */
  		initrd_high = ~0;
  	}
95d449ad4   Marian Balakowicz   Avoid initrd and ...
1070
1071
1072
1073
1074
  
  #ifdef CONFIG_LOGBUFFER
  	/* Prevent initrd from overwriting logbuffer */
  	lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
  #endif
712fbcf38   Stephen Warren   checkpatch whites...
1075
1076
  	debug("## initrd_high = 0x%08lx, copy_to_ram = %d
  ",
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1077
1078
1079
1080
  			initrd_high, initrd_copy_to_ram);
  
  	if (rd_data) {
  		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
712fbcf38   Stephen Warren   checkpatch whites...
1081
1082
  			debug("   in-place initrd
  ");
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1083
1084
  			*initrd_start = rd_data;
  			*initrd_end = rd_data + rd_len;
e822d7fc4   Kumar Gala   [new uImage] Use ...
1085
  			lmb_reserve(lmb, rd_data, rd_len);
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1086
  		} else {
e822d7fc4   Kumar Gala   [new uImage] Use ...
1087
  			if (initrd_high)
712fbcf38   Stephen Warren   checkpatch whites...
1088
1089
  				*initrd_start = (ulong)lmb_alloc_base(lmb,
  						rd_len, 0x1000, initrd_high);
e822d7fc4   Kumar Gala   [new uImage] Use ...
1090
  			else
712fbcf38   Stephen Warren   checkpatch whites...
1091
1092
  				*initrd_start = (ulong)lmb_alloc(lmb, rd_len,
  								 0x1000);
e822d7fc4   Kumar Gala   [new uImage] Use ...
1093
1094
  
  			if (*initrd_start == 0) {
712fbcf38   Stephen Warren   checkpatch whites...
1095
1096
  				puts("ramdisk - allocation error
  ");
e822d7fc4   Kumar Gala   [new uImage] Use ...
1097
  				goto error;
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1098
  			}
770605e4f   Simon Glass   bootstage: Replac...
1099
  			bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1100
1101
  
  			*initrd_end = *initrd_start + rd_len;
712fbcf38   Stephen Warren   checkpatch whites...
1102
  			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1103
  					*initrd_start, *initrd_end);
712fbcf38   Stephen Warren   checkpatch whites...
1104
  			memmove_wd((void *)*initrd_start,
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1105
  					(void *)rd_data, rd_len, CHUNKSZ);
3b2001105   Kumar Gala   powerpc/bootm: Fl...
1106
1107
1108
1109
1110
1111
1112
1113
  #ifdef CONFIG_MP
  			/*
  			 * Ensure the image is flushed to memory to handle
  			 * AMP boot scenarios in which we might not be
  			 * HW cache coherent
  			 */
  			flush_cache((unsigned long)*initrd_start, rd_len);
  #endif
712fbcf38   Stephen Warren   checkpatch whites...
1114
1115
  			puts("OK
  ");
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1116
1117
1118
1119
1120
  		}
  	} else {
  		*initrd_start = 0;
  		*initrd_end = 0;
  	}
712fbcf38   Stephen Warren   checkpatch whites...
1121
1122
  	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx
  ",
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1123
  			*initrd_start, *initrd_end);
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
1124

e822d7fc4   Kumar Gala   [new uImage] Use ...
1125
  	return 0;
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1126

e822d7fc4   Kumar Gala   [new uImage] Use ...
1127
1128
  error:
  	return -1;
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1129
  }
fca43cc80   John Rigby   boot: change some...
1130
  #endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1131

fca43cc80   John Rigby   boot: change some...
1132
  #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1133
  /**
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
1134
   * boot_get_cmdline - allocate and initialize kernel cmdline
e822d7fc4   Kumar Gala   [new uImage] Use ...
1135
   * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1136
1137
1138
   * @cmd_start: pointer to a ulong variable, will hold cmdline start
   * @cmd_end: pointer to a ulong variable, will hold cmdline end
   *
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
1139
   * boot_get_cmdline() allocates space for kernel command line below
590d3cacb   Grant Likely   Stop passing arou...
1140
   * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1141
1142
1143
1144
   * variable is present its contents is copied to allocated kernel
   * command line.
   *
   * returns:
e822d7fc4   Kumar Gala   [new uImage] Use ...
1145
1146
   *      0 - success
   *     -1 - failure
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1147
   */
712fbcf38   Stephen Warren   checkpatch whites...
1148
  int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1149
1150
1151
  {
  	char *cmdline;
  	char *s;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
1152
  	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
c3624e6ed   Grant Likely   Default to bootm_...
1153
  				getenv_bootm_mapsize() + getenv_bootm_low());
e822d7fc4   Kumar Gala   [new uImage] Use ...
1154
1155
1156
  
  	if (cmdline == NULL)
  		return -1;
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1157
1158
1159
1160
1161
1162
1163
1164
  
  	if ((s = getenv("bootargs")) == NULL)
  		s = "";
  
  	strcpy(cmdline, s);
  
  	*cmd_start = (ulong) & cmdline[0];
  	*cmd_end = *cmd_start + strlen(cmdline);
712fbcf38   Stephen Warren   checkpatch whites...
1165
1166
  	debug("## cmdline at 0x%08lx ... 0x%08lx
  ", *cmd_start, *cmd_end);
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1167

e822d7fc4   Kumar Gala   [new uImage] Use ...
1168
  	return 0;
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1169
  }
fca43cc80   John Rigby   boot: change some...
1170
  #endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1171

fca43cc80   John Rigby   boot: change some...
1172
  #ifdef CONFIG_SYS_BOOT_GET_KBD
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1173
  /**
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
1174
   * boot_get_kbd - allocate and initialize kernel copy of board info
e822d7fc4   Kumar Gala   [new uImage] Use ...
1175
   * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1176
1177
   * @kbd: double pointer to board info data
   *
9a4daad0a   Marian Balakowicz   [new uImage] Upda...
1178
   * boot_get_kbd() allocates space for kernel copy of board info data below
590d3cacb   Grant Likely   Stop passing arou...
1179
1180
   * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
   * with the current u-boot board info data.
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1181
1182
   *
   * returns:
e822d7fc4   Kumar Gala   [new uImage] Use ...
1183
1184
   *      0 - success
   *     -1 - failure
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1185
   */
712fbcf38   Stephen Warren   checkpatch whites...
1186
  int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1187
  {
391fd93ab   Becky Bruce   Change lmb to use...
1188
  	*kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
c3624e6ed   Grant Likely   Default to bootm_...
1189
  				getenv_bootm_mapsize() + getenv_bootm_low());
e822d7fc4   Kumar Gala   [new uImage] Use ...
1190
1191
  	if (*kbd == NULL)
  		return -1;
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1192
  	**kbd = *(gd->bd);
712fbcf38   Stephen Warren   checkpatch whites...
1193
1194
  	debug("## kernel board info at 0x%08lx
  ", (ulong)*kbd);
b6b0fe646   Marian Balakowicz   [new uImage] Clea...
1195
1196
1197
1198
  
  #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  	do_bdinfo(NULL, 0, 0, NULL);
  #endif
e822d7fc4   Kumar Gala   [new uImage] Use ...
1199
  	return 0;
ceaed2b1e   Marian Balakowicz   [new uImage] Move...
1200
  }
fca43cc80   John Rigby   boot: change some...
1201
  #endif /* CONFIG_SYS_BOOT_GET_KBD */
13d06981a   Simon Glass   image: Add device...
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
  
  #ifdef CONFIG_LMB
  int image_setup_linux(bootm_headers_t *images)
  {
  	ulong of_size = images->ft_len;
  	char **of_flat_tree = &images->ft_addr;
  	ulong *initrd_start = &images->initrd_start;
  	ulong *initrd_end = &images->initrd_end;
  	struct lmb *lmb = &images->lmb;
  	ulong rd_len;
  	int ret;
  
  	if (IMAGE_ENABLE_OF_LIBFDT)
  		boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  
  	if (IMAGE_BOOT_GET_CMDLINE) {
  		ret = boot_get_cmdline(lmb, &images->cmdline_start,
  				&images->cmdline_end);
  		if (ret) {
  			puts("ERROR with allocation of cmdline
  ");
  			return ret;
  		}
  	}
  	if (IMAGE_ENABLE_RAMDISK_HIGH) {
  		rd_len = images->rd_end - images->rd_start;
  		ret = boot_ramdisk_high(lmb, images->rd_start, rd_len,
  				initrd_start, initrd_end);
  		if (ret)
  			return ret;
  	}
  
  	if (IMAGE_ENABLE_OF_LIBFDT) {
  		ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  		if (ret)
  			return ret;
  	}
  
  	if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
  		ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
  		if (ret)
  			return ret;
  	}
  
  	return 0;
  }
  #endif /* CONFIG_LMB */
5dfb52138   Marian Balakowicz   [new uImage] New ...
1249
  #endif /* !USE_HOSTCC */