image.c 46.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * (C) Copyright 2008 Semihalf
 *
 * (C) Copyright 2000-2006
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */
25

26
#ifndef USE_HOSTCC
27 28 29 30 31 32 33 34 35 36 37
#include <common.h>
#include <watchdog.h>

#ifdef CONFIG_SHOW_BOOT_PROGRESS
#include <status_led.h>
#endif

#ifdef CONFIG_HAS_DATAFLASH
#include <dataflash.h>
#endif

38 39 40 41
#ifdef CONFIG_LOGBUFFER
#include <logbuff.h>
#endif

42 43
#include <rtc.h>

J
Joe Hershberger 已提交
44
#include <environment.h>
45 46
#include <image.h>

S
Stephen Warren 已提交
47
#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
48 49
#include <libfdt.h>
#include <fdt_support.h>
50 51
#endif

52
#include <u-boot/md5.h>
53
#include <sha1.h>
54

55
#ifdef CONFIG_CMD_BDI
56
extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
57 58 59
#endif

DECLARE_GLOBAL_DATA_PTR;
60

S
Stephen Warren 已提交
61
static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
62
						int verify);
63
#else
64
#include "mkimage.h"
65
#include <u-boot/md5.h>
66
#include <time.h>
67
#include <image.h>
68
#endif /* !USE_HOSTCC*/
69

70 71
#include <u-boot/crc.h>

M
Mike Frysinger 已提交
72
static const table_entry_t uimage_arch[] = {
73 74 75 76 77 78 79 80 81 82
	{	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",	},
	{	IH_ARCH_NIOS2,		"nios2",	"NIOS II",	},
83
	{	IH_ARCH_PPC,		"powerpc",	"PowerPC",	},
84 85 86 87 88 89 90
	{	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",	},
91
	{	IH_ARCH_NDS32,		"nds32",	"NDS32",	},
92
	{	IH_ARCH_OPENRISC,	"or1k",		"OpenRISC 1000",},
93 94 95
	{	-1,			"",		"",		},
};

M
Mike Frysinger 已提交
96
static const table_entry_t uimage_os[] = {
97 98 99 100 101 102
	{	IH_OS_INVALID,	NULL,		"Invalid OS",		},
	{	IH_OS_LINUX,	"linux",	"Linux",		},
#if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
	{	IH_OS_LYNXOS,	"lynxos",	"LynxOS",		},
#endif
	{	IH_OS_NETBSD,	"netbsd",	"NetBSD",		},
103
	{	IH_OS_OSE,	"ose",		"Enea OSE",		},
S
Steven Stallion 已提交
104
	{	IH_OS_PLAN9,	"plan9",	"Plan 9",		},
105 106 107 108 109 110
	{	IH_OS_RTEMS,	"rtems",	"RTEMS",		},
	{	IH_OS_U_BOOT,	"u-boot",	"U-Boot",		},
#if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
	{	IH_OS_QNX,	"qnx",		"QNX",			},
	{	IH_OS_VXWORKS,	"vxworks",	"VxWorks",		},
#endif
111 112 113
#if defined(CONFIG_INTEGRITY) || defined(USE_HOSTCC)
	{	IH_OS_INTEGRITY,"integrity",	"INTEGRITY",		},
#endif
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#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,		"",		"",			},
};

M
Mike Frysinger 已提交
130
static const table_entry_t uimage_type[] = {
131
	{	IH_TYPE_AISIMAGE,   "aisimage",   "Davinci AIS image",},
132 133
	{	IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image",	},
	{	IH_TYPE_FIRMWARE,   "firmware",	  "Firmware",		},
134
	{	IH_TYPE_FLATDT,     "flat_dt",    "Flat Device Tree",	},
135
	{	IH_TYPE_KERNEL,	    "kernel",	  "Kernel Image",	},
136
	{	IH_TYPE_KERNEL_NOLOAD, "kernel_noload",  "Kernel Image (no loading done)", },
137 138 139
	{	IH_TYPE_KWBIMAGE,   "kwbimage",   "Kirkwood Boot Image",},
	{	IH_TYPE_IMXIMAGE,   "imximage",   "Freescale i.MX Boot Image",},
	{	IH_TYPE_INVALID,    NULL,	  "Invalid Image",	},
140
	{	IH_TYPE_MULTI,	    "multi",	  "Multi-File Image",	},
141
	{	IH_TYPE_OMAPIMAGE,  "omapimage",  "TI OMAP SPL With GP CH",},
142
	{	IH_TYPE_PBLIMAGE,   "pblimage",   "Freescale PBL Boot Image",},
143 144 145
	{	IH_TYPE_RAMDISK,    "ramdisk",	  "RAMDisk Image",	},
	{	IH_TYPE_SCRIPT,     "script",	  "Script",		},
	{	IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
146
	{	IH_TYPE_UBLIMAGE,   "ublimage",   "Davinci UBL image",},
147 148 149
	{	-1,		    "",		  "",			},
};

M
Mike Frysinger 已提交
150
static const table_entry_t uimage_comp[] = {
151 152 153
	{	IH_COMP_NONE,	"none",		"uncompressed",		},
	{	IH_COMP_BZIP2,	"bzip2",	"bzip2 compressed",	},
	{	IH_COMP_GZIP,	"gzip",		"gzip compressed",	},
154
	{	IH_COMP_LZMA,	"lzma",		"lzma compressed",	},
P
Peter Korsgaard 已提交
155
	{	IH_COMP_LZO,	"lzo",		"lzo compressed",	},
156 157 158
	{	-1,		"",		"",			},
};

159 160 161
/*****************************************************************************/
/* Legacy format routines */
/*****************************************************************************/
S
Stephen Warren 已提交
162
int image_check_hcrc(const image_header_t *hdr)
163 164
{
	ulong hcrc;
S
Stephen Warren 已提交
165
	ulong len = image_get_header_size();
166 167 168
	image_header_t header;

	/* Copy header so we can blank CRC field for re-calculation */
S
Stephen Warren 已提交
169 170
	memmove(&header, (char *)hdr, image_get_header_size());
	image_set_hcrc(&header, 0);
171

S
Stephen Warren 已提交
172
	hcrc = crc32(0, (unsigned char *)&header, len);
173

S
Stephen Warren 已提交
174
	return (hcrc == image_get_hcrc(hdr));
175 176
}

S
Stephen Warren 已提交
177
int image_check_dcrc(const image_header_t *hdr)
178
{
S
Stephen Warren 已提交
179 180 181
	ulong data = image_get_data(hdr);
	ulong len = image_get_data_size(hdr);
	ulong dcrc = crc32_wd(0, (unsigned char *)data, len, CHUNKSZ_CRC32);
182

S
Stephen Warren 已提交
183
	return (dcrc == image_get_dcrc(hdr));
184 185
}

186 187 188 189 190 191 192 193 194 195 196 197 198
/**
 * 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
 */
S
Stephen Warren 已提交
199
ulong image_multi_count(const image_header_t *hdr)
200 201
{
	ulong i, count = 0;
202
	uint32_t *size;
203 204 205

	/* get start of the image payload, which in case of multi
	 * component images that points to a table of component sizes */
S
Stephen Warren 已提交
206
	size = (uint32_t *)image_get_data(hdr);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

	/* 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
 */
S
Stephen Warren 已提交
232
void image_multi_getimg(const image_header_t *hdr, ulong idx,
233 234 235
			ulong *data, ulong *len)
{
	int i;
236
	uint32_t *size;
237
	ulong offset, count, img_data;
238 239

	/* get number of component */
S
Stephen Warren 已提交
240
	count = image_multi_count(hdr);
241 242 243

	/* get start of the image payload, which in case of multi
	 * component images that points to a table of component sizes */
S
Stephen Warren 已提交
244
	size = (uint32_t *)image_get_data(hdr);
245 246 247

	/* get address of the proper component data start, which means
	 * skipping sizes table (add 1 for last, null entry) */
S
Stephen Warren 已提交
248
	img_data = image_get_data(hdr) + (count + 1) * sizeof(uint32_t);
249 250

	if (idx < count) {
S
Stephen Warren 已提交
251
		*len = uimage_to_cpu(size[idx]);
252 253 254 255
		offset = 0;

		/* go over all indices preceding requested component idx */
		for (i = 0; i < idx; i++) {
256
			/* add up i-th component size, rounding up to 4 bytes */
S
Stephen Warren 已提交
257
			offset += (uimage_to_cpu(size[i]) + 3) & ~3 ;
258 259 260
		}

		/* calculate idx-th component data address */
261
		*data = img_data + offset;
262 263 264 265 266
	} else {
		*len = 0;
		*data = 0;
	}
}
267

S
Stephen Warren 已提交
268
static void image_print_type(const image_header_t *hdr)
269 270 271
{
	const char *os, *arch, *type, *comp;

S
Stephen Warren 已提交
272 273 274 275
	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));
276

S
Stephen Warren 已提交
277
	printf("%s %s %s (%s)\n", arch, os, type, comp);
278 279
}

280
/**
281
 * image_print_contents - prints out the contents of the legacy format image
282
 * @ptr: pointer to the legacy format image header
283 284
 * @p: pointer to prefix string
 *
285
 * image_print_contents() formats a multi line legacy image contents description.
286 287 288 289 290 291
 * The routine prints out all header fields followed by the size/offset data
 * for MULTI/SCRIPT images.
 *
 * returns:
 *     no returned results
 */
S
Stephen Warren 已提交
292
void image_print_contents(const void *ptr)
293
{
294
	const image_header_t *hdr = (const image_header_t *)ptr;
295 296 297 298 299 300 301 302
	const char *p;

#ifdef USE_HOSTCC
	p = "";
#else
	p = "   ";
#endif

S
Stephen Warren 已提交
303
	printf("%sImage Name:   %.*s\n", p, IH_NMLEN, image_get_name(hdr));
304 305 306 307
	if (IMAGE_ENABLE_TIMESTAMP) {
		printf("%sCreated:      ", p);
		genimg_print_time((time_t)image_get_time(hdr));
	}
S
Stephen Warren 已提交
308 309 310 311 312 313 314 315 316
	printf("%sImage Type:   ", p);
	image_print_type(hdr);
	printf("%sData Size:    ", p);
	genimg_print_size(image_get_data_size(hdr));
	printf("%sLoad Address: %08x\n", p, image_get_load(hdr));
	printf("%sEntry Point:  %08x\n", p, image_get_ep(hdr));

	if (image_check_type(hdr, IH_TYPE_MULTI) ||
			image_check_type(hdr, IH_TYPE_SCRIPT)) {
317 318
		int i;
		ulong data, len;
S
Stephen Warren 已提交
319
		ulong count = image_multi_count(hdr);
320

S
Stephen Warren 已提交
321
		printf("%sContents:\n", p);
322
		for (i = 0; i < count; i++) {
S
Stephen Warren 已提交
323
			image_multi_getimg(hdr, i, &data, &len);
324

S
Stephen Warren 已提交
325 326
			printf("%s   Image %d: ", p, i);
			genimg_print_size(len);
327

S
Stephen Warren 已提交
328
			if (image_check_type(hdr, IH_TYPE_SCRIPT) && i > 0) {
329 330 331 332 333
				/*
				 * the user may need to know offsets
				 * if planning to do something with
				 * multiple files
				 */
S
Stephen Warren 已提交
334
				printf("%s    Offset = 0x%08lx\n", p, data);
335
			}
336 337 338 339
		}
	}
}

340 341

#ifndef USE_HOSTCC
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
/**
 * image_get_ramdisk - get and verify ramdisk image
 * @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
 */
S
Stephen Warren 已提交
360
static const image_header_t *image_get_ramdisk(ulong rd_addr, uint8_t arch,
361
						int verify)
362
{
363
	const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
364

S
Stephen Warren 已提交
365 366
	if (!image_check_magic(rd_hdr)) {
		puts("Bad Magic Number\n");
367
		bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
368 369 370
		return NULL;
	}

S
Stephen Warren 已提交
371 372
	if (!image_check_hcrc(rd_hdr)) {
		puts("Bad Header Checksum\n");
373
		bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
374 375 376
		return NULL;
	}

377
	bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
S
Stephen Warren 已提交
378
	image_print_contents(rd_hdr);
379 380 381

	if (verify) {
		puts("   Verifying Checksum ... ");
S
Stephen Warren 已提交
382 383
		if (!image_check_dcrc(rd_hdr)) {
			puts("Bad Data CRC\n");
384
			bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
385 386 387 388 389
			return NULL;
		}
		puts("OK\n");
	}

390
	bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
391

S
Stephen Warren 已提交
392 393 394 395
	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\n",
396
				genimg_get_arch_name(arch));
397
		bootstage_error(BOOTSTAGE_ID_RAMDISK);
398 399 400 401 402
		return NULL;
	}

	return rd_hdr;
}
403
#endif /* !USE_HOSTCC */
404 405 406 407

/*****************************************************************************/
/* Shared dual-format routines */
/*****************************************************************************/
408
#ifndef USE_HOSTCC
J
Joe Hershberger 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
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);

429 430
ulong getenv_bootm_low(void)
{
S
Stephen Warren 已提交
431
	char *s = getenv("bootm_low");
432
	if (s) {
S
Stephen Warren 已提交
433
		ulong tmp = simple_strtoul(s, NULL, 16);
434 435 436
		return tmp;
	}

437 438
#if defined(CONFIG_SYS_SDRAM_BASE)
	return CONFIG_SYS_SDRAM_BASE;
439 440
#elif defined(CONFIG_ARM)
	return gd->bd->bi_dram[0].start;
441 442 443 444 445
#else
	return 0;
#endif
}

446
phys_size_t getenv_bootm_size(void)
447
{
448
	phys_size_t tmp;
S
Stephen Warren 已提交
449
	char *s = getenv("bootm_size");
450
	if (s) {
S
Stephen Warren 已提交
451
		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
452 453
		return tmp;
	}
454 455
	s = getenv("bootm_low");
	if (s)
S
Stephen Warren 已提交
456
		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
457 458 459
	else
		tmp = 0;

460

461
#if defined(CONFIG_ARM)
462
	return gd->bd->bi_dram[0].size - tmp;
463
#else
464
	return gd->bd->bi_memsize - tmp;
465
#endif
466 467
}

468 469 470
phys_size_t getenv_bootm_mapsize(void)
{
	phys_size_t tmp;
S
Stephen Warren 已提交
471
	char *s = getenv("bootm_mapsize");
472
	if (s) {
S
Stephen Warren 已提交
473
		tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
474 475 476 477 478 479 480 481 482 483
		return tmp;
	}

#if defined(CONFIG_SYS_BOOTMAPSZ)
	return CONFIG_SYS_BOOTMAPSZ;
#else
	return getenv_bootm_size();
#endif
}

S
Stephen Warren 已提交
484
void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
485
{
486 487 488
	if (to == from)
		return;

489 490 491
#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
	while (len > 0) {
		size_t tail = (len > chunksz) ? chunksz : len;
S
Stephen Warren 已提交
492 493
		WATCHDOG_RESET();
		memmove(to, from, tail);
494 495 496 497 498
		to += tail;
		from += tail;
		len -= tail;
	}
#else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
S
Stephen Warren 已提交
499
	memmove(to, from, len);
500 501
#endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
}
502
#endif /* !USE_HOSTCC */
503

S
Stephen Warren 已提交
504
void genimg_print_size(uint32_t size)
505
{
506
#ifndef USE_HOSTCC
S
Stephen Warren 已提交
507 508
	printf("%d Bytes = ", size);
	print_size(size, "\n");
509
#else
S
Stephen Warren 已提交
510
	printf("%d Bytes = %.2f kB = %.2f MB\n",
511 512
			size, (double)size / 1.024e3,
			(double)size / 1.048576e6);
513
#endif
514 515
}

516 517
#if IMAGE_ENABLE_TIMESTAMP
void genimg_print_time(time_t timestamp)
518 519 520 521
{
#ifndef USE_HOSTCC
	struct rtc_time tm;

S
Stephen Warren 已提交
522 523
	to_tm(timestamp, &tm);
	printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
524 525 526
			tm.tm_year, tm.tm_mon, tm.tm_mday,
			tm.tm_hour, tm.tm_min, tm.tm_sec);
#else
S
Stephen Warren 已提交
527
	printf("%s", ctime(&timestamp));
528
#endif
529
}
530
#endif
531

532 533 534 535 536 537 538 539 540 541 542 543 544 545
/**
 * 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
 */
M
Mike Frysinger 已提交
546
char *get_table_entry_name(const table_entry_t *table, char *msg, int id)
547 548 549
{
	for (; table->id >= 0; ++table) {
		if (table->id == id)
550
#if defined(USE_HOSTCC) || !defined(CONFIG_NEEDS_MANUAL_RELOC)
551 552 553 554
			return table->lname;
#else
			return table->lname + gd->reloc_off;
#endif
555
	}
556 557
	return (msg);
}
558

S
Stephen Warren 已提交
559
const char *genimg_get_os_name(uint8_t os)
560
{
S
Stephen Warren 已提交
561
	return (get_table_entry_name(uimage_os, "Unknown OS", os));
562 563
}

S
Stephen Warren 已提交
564
const char *genimg_get_arch_name(uint8_t arch)
565
{
S
Stephen Warren 已提交
566 567
	return (get_table_entry_name(uimage_arch, "Unknown Architecture",
					arch));
568
}
569

S
Stephen Warren 已提交
570
const char *genimg_get_type_name(uint8_t type)
571
{
S
Stephen Warren 已提交
572
	return (get_table_entry_name(uimage_type, "Unknown Image", type));
573
}
574

S
Stephen Warren 已提交
575
const char *genimg_get_comp_name(uint8_t comp)
576
{
S
Stephen Warren 已提交
577 578
	return (get_table_entry_name(uimage_comp, "Unknown Compression",
					comp));
579 580
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594
/**
 * 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
 */
M
Mike Frysinger 已提交
595
int get_table_entry_id(const table_entry_t *table,
596
		const char *table_name, const char *name)
597
{
M
Mike Frysinger 已提交
598
	const table_entry_t *t;
599 600
#ifdef USE_HOSTCC
	int first = 1;
601

602 603
	for (t = table; t->id >= 0; ++t) {
		if (t->sname && strcasecmp(t->sname, name) == 0)
S
Stephen Warren 已提交
604
			return(t->id);
605 606
	}

S
Stephen Warren 已提交
607
	fprintf(stderr, "\nInvalid %s Type - valid names are", table_name);
608 609 610
	for (t = table; t->id >= 0; ++t) {
		if (t->sname == NULL)
			continue;
S
Stephen Warren 已提交
611
		fprintf(stderr, "%c %s", (first) ? ':' : ',', t->sname);
612 613
		first = 0;
	}
S
Stephen Warren 已提交
614
	fprintf(stderr, "\n");
615 616
#else
	for (t = table; t->id >= 0; ++t) {
617
#ifdef CONFIG_NEEDS_MANUAL_RELOC
618
		if (t->sname && strcmp(t->sname + gd->reloc_off, name) == 0)
619 620
#else
		if (t->sname && strcmp(t->sname, name) == 0)
621
#endif
622 623
			return (t->id);
	}
S
Stephen Warren 已提交
624
	debug("Invalid %s Type: %s\n", table_name, name);
625 626 627 628
#endif /* USE_HOSTCC */
	return (-1);
}

S
Stephen Warren 已提交
629
int genimg_get_os_id(const char *name)
630
{
S
Stephen Warren 已提交
631
	return (get_table_entry_id(uimage_os, "OS", name));
632 633
}

S
Stephen Warren 已提交
634
int genimg_get_arch_id(const char *name)
635
{
S
Stephen Warren 已提交
636
	return (get_table_entry_id(uimage_arch, "CPU", name));
637
}
638

S
Stephen Warren 已提交
639
int genimg_get_type_id(const char *name)
640
{
S
Stephen Warren 已提交
641
	return (get_table_entry_id(uimage_type, "Image", name));
642 643
}

S
Stephen Warren 已提交
644
int genimg_get_comp_id(const char *name)
645
{
S
Stephen Warren 已提交
646
	return (get_table_entry_id(uimage_comp, "Compression", name));
647 648 649
}

#ifndef USE_HOSTCC
650
/**
651
 * genimg_get_format - get image format type
652 653
 * @img_addr: image start address
 *
654
 * genimg_get_format() checks whether provided address points to a valid
655 656
 * legacy or FIT image.
 *
657 658
 * 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
659
 * genimg_get_format() must be able to dectect libfdt header.
660
 *
661 662 663
 * returns:
 *     image format type or IMAGE_FORMAT_INVALID if no image is present
 */
S
Stephen Warren 已提交
664
int genimg_get_format(void *img_addr)
665
{
666 667
	ulong format = IMAGE_FORMAT_INVALID;
	const image_header_t *hdr;
668
#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
669
	char *fit_hdr;
670 671
#endif

672
	hdr = (const image_header_t *)img_addr;
673 674
	if (image_check_magic(hdr))
		format = IMAGE_FORMAT_LEGACY;
675
#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
676 677
	else {
		fit_hdr = (char *)img_addr;
S
Stephen Warren 已提交
678
		if (fdt_check_header(fit_hdr) == 0)
679 680 681 682 683 684 685 686
			format = IMAGE_FORMAT_FIT;
	}
#endif

	return format;
}

/**
687
 * genimg_get_image - get image from special storage (if necessary)
688 689
 * @img_addr: image start address
 *
690
 * genimg_get_image() checks if provided image start adddress is located
691 692 693 694 695
 * in a dataflash storage. If so, image is moved to a system RAM memory.
 *
 * returns:
 *     image start address after possible relocation from special storage
 */
S
Stephen Warren 已提交
696
ulong genimg_get_image(ulong img_addr)
697
{
698
	ulong ram_addr = img_addr;
699 700

#ifdef CONFIG_HAS_DATAFLASH
701 702
	ulong h_size, d_size;

S
Stephen Warren 已提交
703
	if (addr_dataflash(img_addr)) {
704
		/* ger RAM address */
705
		ram_addr = CONFIG_SYS_LOAD_ADDR;
706 707

		/* get header size */
S
Stephen Warren 已提交
708
		h_size = image_get_header_size();
709 710 711 712 713 714
#if defined(CONFIG_FIT)
		if (sizeof(struct fdt_header) > h_size)
			h_size = sizeof(struct fdt_header);
#endif

		/* read in header */
S
Stephen Warren 已提交
715
		debug("   Reading image header from dataflash address "
716 717
			"%08lx to RAM address %08lx\n", img_addr, ram_addr);

S
Stephen Warren 已提交
718
		read_dataflash(img_addr, h_size, (char *)ram_addr);
719

720
		/* get data size */
S
Stephen Warren 已提交
721
		switch (genimg_get_format((void *)ram_addr)) {
722
		case IMAGE_FORMAT_LEGACY:
S
Stephen Warren 已提交
723 724 725 726
			d_size = image_get_data_size(
					(const image_header_t *)ram_addr);
			debug("   Legacy format image found at 0x%08lx, "
					"size 0x%08lx\n",
727 728
					ram_addr, d_size);
			break;
729
#if defined(CONFIG_FIT)
730
		case IMAGE_FORMAT_FIT:
S
Stephen Warren 已提交
731 732 733
			d_size = fit_get_size((const void *)ram_addr) - h_size;
			debug("   FIT/FDT format image found at 0x%08lx, "
					"size 0x%08lx\n",
734 735
					ram_addr, d_size);
			break;
736
#endif
737
		default:
S
Stephen Warren 已提交
738 739
			printf("   No valid image found at 0x%08lx\n",
				img_addr);
740 741
			return ram_addr;
		}
742

743
		/* read in image data */
S
Stephen Warren 已提交
744
		debug("   Reading image remaining data from dataflash address "
745 746 747
			"%08lx to RAM address %08lx\n", img_addr + h_size,
			ram_addr + h_size);

S
Stephen Warren 已提交
748
		read_dataflash(img_addr + h_size, d_size,
749
				(char *)(ram_addr + h_size));
750

751
	}
752
#endif /* CONFIG_HAS_DATAFLASH */
753 754 755 756

	return ram_addr;
}

757 758 759 760 761 762 763 764 765 766 767
/**
 * 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
 */
S
Stephen Warren 已提交
768
int genimg_has_config(bootm_headers_t *images)
769 770 771 772 773 774 775 776
{
#if defined(CONFIG_FIT)
	if (images->fit_uname_cfg)
		return 1;
#endif
	return 0;
}

777
/**
778
 * boot_get_ramdisk - main ramdisk handling routine
779 780
 * @argc: command argument count
 * @argv: command argument list
781
 * @images: pointer to the bootm images structure
782 783 784 785
 * @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
 *
786
 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
787 788 789 790 791
 * Curently supported are the following ramdisk sources:
 *      - multicomponent kernel/ramdisk image,
 *      - commandline provided address of decicated ramdisk image.
 *
 * returns:
792
 *     0, if ramdisk image was found and valid, or skiped
793 794
 *     rd_start and rd_end are set to ramdisk start/end addresses if
 *     ramdisk image is found and valid
795
 *
796
 *     1, if ramdisk image is found but corrupted, or invalid
797 798
 *     rd_start and rd_end are set to 0 if no ramdisk exists
 */
S
Stephen Warren 已提交
799
int boot_get_ramdisk(int argc, char * const argv[], bootm_headers_t *images,
800
		uint8_t arch, ulong *rd_start, ulong *rd_end)
801
{
802
	ulong rd_addr, rd_load;
803
	ulong rd_data, rd_len;
804
	const image_header_t *rd_hdr;
805
#ifdef CONFIG_SUPPORT_RAW_INITRD
806
	char *end;
807
#endif
808 809 810 811 812
#if defined(CONFIG_FIT)
	void		*fit_hdr;
	const char	*fit_uname_config = NULL;
	const char	*fit_uname_ramdisk = NULL;
	ulong		default_addr;
813
	int		rd_noffset;
814
	int		cfg_noffset;
815 816
	const void	*data;
	size_t		size;
817
#endif
818

819 820 821
	*rd_start = 0;
	*rd_end = 0;

822 823 824 825 826
	/*
	 * Look for a '-' which indicates to ignore the
	 * ramdisk argument
	 */
	if ((argc >= 3) && (strcmp(argv[2], "-") ==  0)) {
S
Stephen Warren 已提交
827
		debug("## Skipping init Ramdisk\n");
828
		rd_len = rd_data = 0;
S
Stephen Warren 已提交
829
	} else if (argc >= 3 || genimg_has_config(images)) {
830
#if defined(CONFIG_FIT)
831 832 833 834 835 836 837 838 839 840 841 842
		if (argc >= 3) {
			/*
			 * 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;

S
Stephen Warren 已提交
843
			if (fit_parse_conf(argv[2], default_addr,
844
						&rd_addr, &fit_uname_config)) {
S
Stephen Warren 已提交
845 846
				debug("*  ramdisk: config '%s' from image at "
						"0x%08lx\n",
847
						fit_uname_config, rd_addr);
S
Stephen Warren 已提交
848
			} else if (fit_parse_subimage(argv[2], default_addr,
849
						&rd_addr, &fit_uname_ramdisk)) {
S
Stephen Warren 已提交
850 851
				debug("*  ramdisk: subimage '%s' from image at "
						"0x%08lx\n",
852 853
						fit_uname_ramdisk, rd_addr);
			} else
854
#endif
855 856
			{
				rd_addr = simple_strtoul(argv[2], NULL, 16);
S
Stephen Warren 已提交
857 858
				debug("*  ramdisk: cmdline image address = "
						"0x%08lx\n",
859 860 861 862 863 864 865 866 867
						rd_addr);
			}
#if defined(CONFIG_FIT)
		} else {
			/* use FIT configuration provided in first bootm
			 * command argument
			 */
			rd_addr = (ulong)images->fit_hdr_os;
			fit_uname_config = images->fit_uname_cfg;
S
Stephen Warren 已提交
868 869
			debug("*  ramdisk: using config '%s' from image "
					"at 0x%08lx\n",
870 871 872 873 874 875 876
					fit_uname_config, rd_addr);

			/*
			 * Check whether configuration has ramdisk defined,
			 * if not, don't try to use it, quit silently.
			 */
			fit_hdr = (void *)rd_addr;
S
Stephen Warren 已提交
877 878
			cfg_noffset = fit_conf_get_node(fit_hdr,
							fit_uname_config);
879
			if (cfg_noffset < 0) {
S
Stephen Warren 已提交
880
				debug("*  ramdisk: no such config\n");
881
				return 1;
882 883
			}

S
Stephen Warren 已提交
884 885
			rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
								cfg_noffset);
886
			if (rd_noffset < 0) {
S
Stephen Warren 已提交
887
				debug("*  ramdisk: no ramdisk in config\n");
888
				return 0;
889
			}
890
		}
891
#endif
892 893

		/* copy from dataflash if needed */
S
Stephen Warren 已提交
894
		rd_addr = genimg_get_image(rd_addr);
895 896 897 898 899 900

		/*
		 * 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.
		 */
S
Stephen Warren 已提交
901
		switch (genimg_get_format((void *)rd_addr)) {
902
		case IMAGE_FORMAT_LEGACY:
S
Stephen Warren 已提交
903
			printf("## Loading init Ramdisk from Legacy "
904
					"Image at %08lx ...\n", rd_addr);
905

906
			bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
S
Stephen Warren 已提交
907
			rd_hdr = image_get_ramdisk(rd_addr, arch,
908
							images->verify);
909

910
			if (rd_hdr == NULL)
911 912
				return 1;

S
Stephen Warren 已提交
913 914 915
			rd_data = image_get_data(rd_hdr);
			rd_len = image_get_data_size(rd_hdr);
			rd_load = image_get_load(rd_hdr);
916 917 918 919
			break;
#if defined(CONFIG_FIT)
		case IMAGE_FORMAT_FIT:
			fit_hdr = (void *)rd_addr;
S
Stephen Warren 已提交
920
			printf("## Loading init Ramdisk from FIT "
921 922
					"Image at %08lx ...\n", rd_addr);

923
			bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT);
S
Stephen Warren 已提交
924 925
			if (!fit_check_format(fit_hdr)) {
				puts("Bad FIT ramdisk image format!\n");
926
				bootstage_error(
927
					BOOTSTAGE_ID_FIT_RD_FORMAT);
928
				return 1;
929
			}
930
			bootstage_mark(BOOTSTAGE_ID_FIT_RD_FORMAT_OK);
931 932 933 934 935 936 937

			if (!fit_uname_ramdisk) {
				/*
				 * no ramdisk image node unit name, try to get config
				 * node first. If config unit node name is NULL
				 * fit_conf_get_node() will try to find default config node
				 */
938
				bootstage_mark(
939
					BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
S
Stephen Warren 已提交
940 941
				cfg_noffset = fit_conf_get_node(fit_hdr,
							fit_uname_config);
942
				if (cfg_noffset < 0) {
S
Stephen Warren 已提交
943 944
					puts("Could not find configuration "
						"node\n");
945
					bootstage_error(
946
					BOOTSTAGE_ID_FIT_RD_NO_UNIT_NAME);
947
					return 1;
948
				}
S
Stephen Warren 已提交
949 950 951 952
				fit_uname_config = fdt_get_name(fit_hdr,
							cfg_noffset, NULL);
				printf("   Using '%s' configuration\n",
					fit_uname_config);
953

S
Stephen Warren 已提交
954 955 956 957
				rd_noffset = fit_conf_get_ramdisk_node(fit_hdr,
							cfg_noffset);
				fit_uname_ramdisk = fit_get_name(fit_hdr,
							rd_noffset, NULL);
958 959
			} else {
				/* get ramdisk component image node offset */
960
				bootstage_mark(
961
					BOOTSTAGE_ID_FIT_RD_UNIT_NAME);
S
Stephen Warren 已提交
962 963
				rd_noffset = fit_image_get_node(fit_hdr,
						fit_uname_ramdisk);
964
			}
965
			if (rd_noffset < 0) {
S
Stephen Warren 已提交
966
				puts("Could not find subimage node\n");
967
				bootstage_error(BOOTSTAGE_ID_FIT_RD_SUBNODE);
968
				return 1;
969
			}
970

S
Stephen Warren 已提交
971 972
			printf("   Trying '%s' ramdisk subimage\n",
				fit_uname_ramdisk);
973

974
			bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK);
S
Stephen Warren 已提交
975 976
			if (!fit_check_ramdisk(fit_hdr, rd_noffset, arch,
						images->verify))
977
				return 1;
978 979

			/* get ramdisk image data address and length */
S
Stephen Warren 已提交
980 981 982
			if (fit_image_get_data(fit_hdr, rd_noffset, &data,
						&size)) {
				puts("Could not find ramdisk subimage data!\n");
983
				bootstage_error(BOOTSTAGE_ID_FIT_RD_GET_DATA);
984
				return 1;
985
			}
986
			bootstage_mark(BOOTSTAGE_ID_FIT_RD_GET_DATA_OK);
987 988 989 990

			rd_data = (ulong)data;
			rd_len = size;

S
Stephen Warren 已提交
991 992 993
			if (fit_image_get_load(fit_hdr, rd_noffset, &rd_load)) {
				puts("Can't get ramdisk subimage load "
					"address!\n");
994
				bootstage_error(BOOTSTAGE_ID_FIT_RD_LOAD);
995
				return 1;
996
			}
997
			bootstage_mark(BOOTSTAGE_ID_FIT_RD_LOAD);
998 999 1000

			images->fit_hdr_rd = fit_hdr;
			images->fit_uname_rd = fit_uname_ramdisk;
1001
			images->fit_noffset_rd = rd_noffset;
1002
			break;
1003 1004
#endif
		default:
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
#ifdef CONFIG_SUPPORT_RAW_INITRD
			if (argc >= 3 && (end = strchr(argv[2], ':'))) {
				rd_len = simple_strtoul(++end, NULL, 16);
				rd_data = rd_addr;
			} else
#endif
			{
				puts("Wrong Ramdisk Image Format\n");
				rd_data = rd_len = rd_load = 0;
				return 1;
			}
1016 1017
		}
	} else if (images->legacy_hdr_valid &&
S
Stephen Warren 已提交
1018 1019 1020
			image_check_type(&images->legacy_hdr_os_copy,
						IH_TYPE_MULTI)) {

1021
		/*
1022 1023
		 * Now check if we have a legacy mult-component image,
		 * get second entry data start address and len.
1024
		 */
1025
		bootstage_mark(BOOTSTAGE_ID_RAMDISK);
S
Stephen Warren 已提交
1026
		printf("## Loading init Ramdisk from multi component "
1027
				"Legacy Image at %08lx ...\n",
1028 1029
				(ulong)images->legacy_hdr_os);

S
Stephen Warren 已提交
1030
		image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
1031 1032 1033 1034
	} else {
		/*
		 * no initrd image
		 */
1035
		bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
1036 1037 1038 1039
		rd_len = rd_data = 0;
	}

	if (!rd_data) {
S
Stephen Warren 已提交
1040
		debug("## No init Ramdisk\n");
1041 1042 1043 1044
	} else {
		*rd_start = rd_data;
		*rd_end = rd_data + rd_len;
	}
S
Stephen Warren 已提交
1045
	debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
1046
			*rd_start, *rd_end);
1047 1048

	return 0;
1049
}
1050

1051
#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1052
/**
1053
 * boot_ramdisk_high - relocate init ramdisk
1054
 * @lmb: pointer to lmb handle, will be used for memory mgmt
1055 1056 1057 1058 1059 1060 1061
 * @rd_data: ramdisk data start address
 * @rd_len: ramdisk data length
 * @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)
 *
1062
 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
1063 1064
 * variable and if requested ramdisk data is moved to a specified location.
 *
1065 1066 1067 1068
 * 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.
 *
1069
 * returns:
1070 1071
 *      0 - success
 *     -1 - failure
1072
 */
S
Stephen Warren 已提交
1073
int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
1074
		  ulong *initrd_start, ulong *initrd_end)
1075 1076 1077 1078 1079
{
	char	*s;
	ulong	initrd_high;
	int	initrd_copy_to_ram = 1;

S
Stephen Warren 已提交
1080
	if ((s = getenv("initrd_high")) != NULL) {
1081 1082 1083
		/* a value of "no" or a similar string will act like 0,
		 * turning the "load high" feature off. This is intentional.
		 */
S
Stephen Warren 已提交
1084
		initrd_high = simple_strtoul(s, NULL, 16);
1085 1086 1087 1088 1089 1090 1091
		if (initrd_high == ~0)
			initrd_copy_to_ram = 0;
	} else {
		/* not set, no restrictions to load high */
		initrd_high = ~0;
	}

1092 1093 1094 1095 1096 1097

#ifdef CONFIG_LOGBUFFER
	/* Prevent initrd from overwriting logbuffer */
	lmb_reserve(lmb, logbuffer_base() - LOGBUFF_OVERHEAD, LOGBUFF_RESERVE);
#endif

S
Stephen Warren 已提交
1098
	debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
1099 1100 1101 1102
			initrd_high, initrd_copy_to_ram);

	if (rd_data) {
		if (!initrd_copy_to_ram) {	/* zero-copy ramdisk support */
S
Stephen Warren 已提交
1103
			debug("   in-place initrd\n");
1104 1105
			*initrd_start = rd_data;
			*initrd_end = rd_data + rd_len;
1106
			lmb_reserve(lmb, rd_data, rd_len);
1107
		} else {
1108
			if (initrd_high)
S
Stephen Warren 已提交
1109 1110
				*initrd_start = (ulong)lmb_alloc_base(lmb,
						rd_len, 0x1000, initrd_high);
1111
			else
S
Stephen Warren 已提交
1112 1113
				*initrd_start = (ulong)lmb_alloc(lmb, rd_len,
								 0x1000);
1114 1115

			if (*initrd_start == 0) {
S
Stephen Warren 已提交
1116
				puts("ramdisk - allocation error\n");
1117
				goto error;
1118
			}
1119
			bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
1120 1121

			*initrd_end = *initrd_start + rd_len;
S
Stephen Warren 已提交
1122
			printf("   Loading Ramdisk to %08lx, end %08lx ... ",
1123 1124
					*initrd_start, *initrd_end);

S
Stephen Warren 已提交
1125
			memmove_wd((void *)*initrd_start,
1126 1127
					(void *)rd_data, rd_len, CHUNKSZ);

1128 1129 1130 1131 1132 1133 1134 1135
#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
S
Stephen Warren 已提交
1136
			puts("OK\n");
1137 1138 1139 1140 1141
		}
	} else {
		*initrd_start = 0;
		*initrd_end = 0;
	}
S
Stephen Warren 已提交
1142
	debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
1143
			*initrd_start, *initrd_end);
1144

1145
	return 0;
1146

1147 1148
error:
	return -1;
1149
}
1150
#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
1151

1152
#ifdef CONFIG_OF_LIBFDT
S
Stephen Warren 已提交
1153
static void fdt_error(const char *msg)
1154
{
S
Stephen Warren 已提交
1155 1156 1157
	puts("ERROR: ");
	puts(msg);
	puts(" - must RESET the board to recover.\n");
1158 1159
}

S
Stephen Warren 已提交
1160
static const image_header_t *image_get_fdt(ulong fdt_addr)
1161
{
1162
	const image_header_t *fdt_hdr = (const image_header_t *)fdt_addr;
1163

S
Stephen Warren 已提交
1164
	image_print_contents(fdt_hdr);
1165

S
Stephen Warren 已提交
1166 1167 1168
	puts("   Verifying Checksum ... ");
	if (!image_check_hcrc(fdt_hdr)) {
		fdt_error("fdt header checksum invalid");
1169 1170 1171
		return NULL;
	}

S
Stephen Warren 已提交
1172 1173
	if (!image_check_dcrc(fdt_hdr)) {
		fdt_error("fdt checksum invalid");
1174 1175
		return NULL;
	}
S
Stephen Warren 已提交
1176
	puts("OK\n");
1177

S
Stephen Warren 已提交
1178 1179
	if (!image_check_type(fdt_hdr, IH_TYPE_FLATDT)) {
		fdt_error("uImage is not a fdt");
1180 1181
		return NULL;
	}
S
Stephen Warren 已提交
1182 1183
	if (image_get_comp(fdt_hdr) != IH_COMP_NONE) {
		fdt_error("uImage is compressed");
1184 1185
		return NULL;
	}
S
Stephen Warren 已提交
1186 1187
	if (fdt_check_header((char *)image_get_data(fdt_hdr)) != 0) {
		fdt_error("uImage data is not a fdt");
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
		return NULL;
	}
	return fdt_hdr;
}

/**
 * fit_check_fdt - verify FIT format FDT subimage
 * @fit_hdr: pointer to the FIT  header
 * fdt_noffset: FDT subimage node offset within FIT image
 * @verify: data CRC verification flag
 *
 * fit_check_fdt() verifies integrity of the FDT subimage and from
 * specified FIT image.
 *
 * returns:
 *     1, on success
 *     0, on failure
 */
#if defined(CONFIG_FIT)
S
Stephen Warren 已提交
1207
static int fit_check_fdt(const void *fit, int fdt_noffset, int verify)
1208
{
S
Stephen Warren 已提交
1209
	fit_image_print(fit, fdt_noffset, "   ");
1210 1211

	if (verify) {
S
Stephen Warren 已提交
1212
		puts("   Verifying Hash Integrity ... ");
1213
		if (!fit_image_verify(fit, fdt_noffset)) {
S
Stephen Warren 已提交
1214
			fdt_error("Bad Data Hash");
1215 1216
			return 0;
		}
S
Stephen Warren 已提交
1217
		puts("OK\n");
1218 1219
	}

S
Stephen Warren 已提交
1220 1221
	if (!fit_image_check_type(fit, fdt_noffset, IH_TYPE_FLATDT)) {
		fdt_error("Not a FDT image");
1222 1223 1224
		return 0;
	}

S
Stephen Warren 已提交
1225 1226
	if (!fit_image_check_comp(fit, fdt_noffset, IH_COMP_NONE)) {
		fdt_error("FDT image is compressed");
1227 1228 1229 1230 1231 1232 1233
		return 0;
	}

	return 1;
}
#endif /* CONFIG_FIT */

1234 1235
#ifndef CONFIG_SYS_FDT_PAD
#define CONFIG_SYS_FDT_PAD 0x3000
1236 1237
#endif

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
#if defined(CONFIG_OF_LIBFDT)
/**
 * boot_fdt_add_mem_rsv_regions - Mark the memreserve sections as unusable
 * @lmb: pointer to lmb handle, will be used for memory mgmt
 * @fdt_blob: pointer to fdt blob base address
 *
 * Adds the memreserve regions in the dtb to the lmb block.  Adding the
 * memreserve regions prevents u-boot from using them to store the initrd
 * or the fdt blob.
 */
void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob)
{
	uint64_t addr, size;
	int i, total;

S
Stephen Warren 已提交
1253
	if (fdt_check_header(fdt_blob) != 0)
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
		return;

	total = fdt_num_mem_rsv(fdt_blob);
	for (i = 0; i < total; i++) {
		if (fdt_get_mem_rsv(fdt_blob, i, &addr, &size) != 0)
			continue;
		printf("   reserving fdt memory region: addr=%llx size=%llx\n",
			(unsigned long long)addr, (unsigned long long)size);
		lmb_reserve(lmb, addr, size);
	}
}

1266 1267 1268 1269 1270 1271
/**
 * boot_relocate_fdt - relocate flat device tree
 * @lmb: pointer to lmb handle, will be used for memory mgmt
 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
 * @of_size: pointer to a ulong variable, will hold fdt length
 *
1272 1273 1274 1275
 * boot_relocate_fdt() allocates a region of memory within the bootmap and
 * relocates the of_flat_tree into that region, even if the fdt is already in
 * the bootmap.  It also expands the size of the fdt by CONFIG_SYS_FDT_PAD
 * bytes.
1276 1277 1278 1279 1280 1281 1282
 *
 * of_flat_tree and of_size are set to final (after relocation) values
 *
 * returns:
 *      0 - success
 *      1 - failure
 */
S
Stephen Warren 已提交
1283
int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size)
1284
{
1285
	void	*fdt_blob = *of_flat_tree;
K
Kim Phillips 已提交
1286
	void	*of_start = NULL;
1287
	char	*fdt_high;
1288
	ulong	of_len = 0;
1289
	int	err;
1290
	int	disable_relocation = 0;
1291 1292 1293 1294 1295

	/* nothing to do */
	if (*of_size == 0)
		return 0;

S
Stephen Warren 已提交
1296 1297
	if (fdt_check_header(fdt_blob) != 0) {
		fdt_error("image is not a fdt");
1298 1299 1300
		goto error;
	}

1301 1302 1303
	/* position on a 4K boundary before the alloc_current */
	/* Pad the FDT by a specified amount */
	of_len = *of_size + CONFIG_SYS_FDT_PAD;
1304 1305 1306 1307 1308 1309 1310 1311

	/* If fdt_high is set use it to select the relocation address */
	fdt_high = getenv("fdt_high");
	if (fdt_high) {
		void *desired_addr = (void *)simple_strtoul(fdt_high, NULL, 16);

		if (((ulong) desired_addr) == ~0UL) {
			/* All ones means use fdt in place */
1312 1313
			of_start = fdt_blob;
			lmb_reserve(lmb, (ulong)of_start, of_len);
1314
			disable_relocation = 1;
1315
		} else if (desired_addr) {
1316 1317
			of_start =
			    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
1318
							   (ulong)desired_addr);
K
Kim Phillips 已提交
1319
			if (of_start == NULL) {
1320 1321 1322 1323 1324
				puts("Failed using fdt_high value for Device Tree");
				goto error;
			}
		} else {
			of_start =
1325
			    (void *)(ulong) lmb_alloc(lmb, of_len, 0x1000);
1326 1327 1328 1329 1330 1331 1332
		}
	} else {
		of_start =
		    (void *)(ulong) lmb_alloc_base(lmb, of_len, 0x1000,
						   getenv_bootm_mapsize()
						   + getenv_bootm_low());
	}
1333

K
Kim Phillips 已提交
1334
	if (of_start == NULL) {
1335 1336 1337
		puts("device tree - allocation error\n");
		goto error;
	}
1338

1339 1340 1341 1342 1343 1344
	if (disable_relocation) {
		/* We assume there is space after the existing fdt to use for padding */
		fdt_set_totalsize(of_start, of_len);
		printf("   Using Device Tree in place at %p, end %p\n",
		       of_start, of_start + of_len - 1);
	} else {
S
Stephen Warren 已提交
1345
		debug("## device tree at %p ... %p (len=%ld [0x%lX])\n",
1346
			fdt_blob, fdt_blob + *of_size - 1, of_len, of_len);
1347

S
Stephen Warren 已提交
1348
		printf("   Loading Device Tree to %p, end %p ... ",
1349
			of_start, of_start + of_len - 1);
1350

S
Stephen Warren 已提交
1351
		err = fdt_open_into(fdt_blob, of_start, of_len);
1352
		if (err != 0) {
S
Stephen Warren 已提交
1353
			fdt_error("fdt move failed");
1354 1355
			goto error;
		}
S
Stephen Warren 已提交
1356
		puts("OK\n");
1357
	}
1358 1359 1360

	*of_flat_tree = of_start;
	*of_size = of_len;
1361

1362
	set_working_fdt_addr(*of_flat_tree);
1363 1364 1365 1366 1367
	return 0;

error:
	return 1;
}
1368
#endif /* CONFIG_OF_LIBFDT */
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

/**
 * boot_get_fdt - main fdt handling routine
 * @argc: command argument count
 * @argv: command argument list
 * @images: pointer to the bootm images structure
 * @of_flat_tree: pointer to a char* variable, will hold fdt start address
 * @of_size: pointer to a ulong variable, will hold fdt length
 *
 * boot_get_fdt() is responsible for finding a valid flat device tree image.
 * Curently supported are the following ramdisk sources:
 *      - multicomponent kernel/ramdisk image,
 *      - commandline provided address of decicated ramdisk image.
 *
 * returns:
 *     0, if fdt image was found and valid, or skipped
 *     of_flat_tree and of_size are set to fdt start address and length if
 *     fdt image is found and valid
 *
 *     1, if fdt image is found but corrupted
 *     of_flat_tree and of_size are set to 0 if no fdt exists
 */
S
Stephen Warren 已提交
1391 1392
int boot_get_fdt(int flag, int argc, char * const argv[],
		bootm_headers_t *images, char **of_flat_tree, ulong *of_size)
1393
{
1394
	const image_header_t *fdt_hdr;
1395 1396
	ulong		fdt_addr;
	char		*fdt_blob = NULL;
1397
	ulong		image_start, image_data, image_end;
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
	ulong		load_start, load_end;
#if defined(CONFIG_FIT)
	void		*fit_hdr;
	const char	*fit_uname_config = NULL;
	const char	*fit_uname_fdt = NULL;
	ulong		default_addr;
	int		cfg_noffset;
	int		fdt_noffset;
	const void	*data;
	size_t		size;
#endif

	*of_flat_tree = NULL;
	*of_size = 0;

S
Stephen Warren 已提交
1413
	if (argc > 3 || genimg_has_config(images)) {
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
#if defined(CONFIG_FIT)
		if (argc > 3) {
			/*
			 * If the FDT blob comes from the FIT image and the
			 * FIT image address is omitted in the command line
			 * argument, try to use ramdisk or os FIT image
			 * address or default load address.
			 */
			if (images->fit_uname_rd)
				default_addr = (ulong)images->fit_hdr_rd;
			else if (images->fit_uname_os)
				default_addr = (ulong)images->fit_hdr_os;
			else
				default_addr = load_addr;

S
Stephen Warren 已提交
1429
			if (fit_parse_conf(argv[3], default_addr,
1430
						&fdt_addr, &fit_uname_config)) {
S
Stephen Warren 已提交
1431 1432
				debug("*  fdt: config '%s' from image at "
						"0x%08lx\n",
1433
						fit_uname_config, fdt_addr);
S
Stephen Warren 已提交
1434
			} else if (fit_parse_subimage(argv[3], default_addr,
1435
						&fdt_addr, &fit_uname_fdt)) {
S
Stephen Warren 已提交
1436 1437
				debug("*  fdt: subimage '%s' from image at "
						"0x%08lx\n",
1438 1439 1440 1441 1442
						fit_uname_fdt, fdt_addr);
			} else
#endif
			{
				fdt_addr = simple_strtoul(argv[3], NULL, 16);
S
Stephen Warren 已提交
1443 1444
				debug("*  fdt: cmdline image address = "
						"0x%08lx\n",
1445 1446 1447 1448 1449 1450 1451 1452 1453
						fdt_addr);
			}
#if defined(CONFIG_FIT)
		} else {
			/* use FIT configuration provided in first bootm
			 * command argument
			 */
			fdt_addr = (ulong)images->fit_hdr_os;
			fit_uname_config = images->fit_uname_cfg;
S
Stephen Warren 已提交
1454 1455
			debug("*  fdt: using config '%s' from image "
					"at 0x%08lx\n",
1456 1457 1458 1459 1460 1461 1462
					fit_uname_config, fdt_addr);

			/*
			 * Check whether configuration has FDT blob defined,
			 * if not quit silently.
			 */
			fit_hdr = (void *)fdt_addr;
S
Stephen Warren 已提交
1463
			cfg_noffset = fit_conf_get_node(fit_hdr,
1464 1465
					fit_uname_config);
			if (cfg_noffset < 0) {
S
Stephen Warren 已提交
1466
				debug("*  fdt: no such config\n");
1467 1468 1469
				return 0;
			}

S
Stephen Warren 已提交
1470
			fdt_noffset = fit_conf_get_fdt_node(fit_hdr,
1471 1472
					cfg_noffset);
			if (fdt_noffset < 0) {
S
Stephen Warren 已提交
1473
				debug("*  fdt: no fdt in config\n");
1474 1475 1476 1477 1478
				return 0;
			}
		}
#endif

S
Stephen Warren 已提交
1479
		debug("## Checking for 'FDT'/'FDT Image' at %08lx\n",
1480 1481 1482
				fdt_addr);

		/* copy from dataflash if needed */
S
Stephen Warren 已提交
1483
		fdt_addr = genimg_get_image(fdt_addr);
1484 1485 1486 1487 1488 1489

		/*
		 * Check if there is an FDT image at the
		 * address provided in the second bootm argument
		 * check image type, for FIT images get a FIT node.
		 */
S
Stephen Warren 已提交
1490
		switch (genimg_get_format((void *)fdt_addr)) {
1491 1492
		case IMAGE_FORMAT_LEGACY:
			/* verify fdt_addr points to a valid image header */
S
Stephen Warren 已提交
1493 1494
			printf("## Flattened Device Tree from Legacy Image "
					"at %08lx\n",
1495
					fdt_addr);
S
Stephen Warren 已提交
1496
			fdt_hdr = image_get_fdt(fdt_addr);
1497 1498 1499 1500 1501 1502 1503 1504
			if (!fdt_hdr)
				goto error;

			/*
			 * move image data to the load address,
			 * make sure we don't overwrite initial image
			 */
			image_start = (ulong)fdt_hdr;
1505
			image_data = (ulong)image_get_data(fdt_hdr);
S
Stephen Warren 已提交
1506
			image_end = image_get_image_end(fdt_hdr);
1507

S
Stephen Warren 已提交
1508 1509
			load_start = image_get_load(fdt_hdr);
			load_end = load_start + image_get_data_size(fdt_hdr);
1510

1511 1512 1513 1514 1515 1516
			if (load_start == image_start ||
			    load_start == image_data) {
				fdt_blob = (char *)image_data;
				break;
			}

1517
			if ((load_start < image_end) && (load_end > image_start)) {
S
Stephen Warren 已提交
1518
				fdt_error("fdt overwritten");
1519 1520 1521
				goto error;
			}

S
Stephen Warren 已提交
1522
			debug("   Loading FDT from 0x%08lx to 0x%08lx\n",
1523
					image_data, load_start);
1524

S
Stephen Warren 已提交
1525
			memmove((void *)load_start,
1526
					(void *)image_data,
S
Stephen Warren 已提交
1527
					image_get_data_size(fdt_hdr));
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538

			fdt_blob = (char *)load_start;
			break;
		case IMAGE_FORMAT_FIT:
			/*
			 * This case will catch both: new uImage format
			 * (libfdt based) and raw FDT blob (also libfdt
			 * based).
			 */
#if defined(CONFIG_FIT)
			/* check FDT blob vs FIT blob */
S
Stephen Warren 已提交
1539
			if (fit_check_format((const void *)fdt_addr)) {
1540 1541 1542 1543
				/*
				 * FIT image
				 */
				fit_hdr = (void *)fdt_addr;
S
Stephen Warren 已提交
1544 1545
				printf("## Flattened Device Tree from FIT "
						"Image at %08lx\n",
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
						fdt_addr);

				if (!fit_uname_fdt) {
					/*
					 * no FDT blob image node unit name,
					 * try to get config node first. If
					 * config unit node name is NULL
					 * fit_conf_get_node() will try to
					 * find default config node
					 */
S
Stephen Warren 已提交
1556
					cfg_noffset = fit_conf_get_node(fit_hdr,
1557 1558 1559
							fit_uname_config);

					if (cfg_noffset < 0) {
S
Stephen Warren 已提交
1560 1561 1562
						fdt_error("Could not find "
							    "configuration "
							    "node\n");
1563 1564 1565
						goto error;
					}

S
Stephen Warren 已提交
1566
					fit_uname_config = fdt_get_name(fit_hdr,
1567
							cfg_noffset, NULL);
S
Stephen Warren 已提交
1568
					printf("   Using '%s' configuration\n",
1569 1570
							fit_uname_config);

S
Stephen Warren 已提交
1571 1572
					fdt_noffset = fit_conf_get_fdt_node(
							fit_hdr,
1573
							cfg_noffset);
S
Stephen Warren 已提交
1574
					fit_uname_fdt = fit_get_name(fit_hdr,
1575 1576 1577
							fdt_noffset, NULL);
				} else {
					/* get FDT component image node offset */
S
Stephen Warren 已提交
1578 1579 1580
					fdt_noffset = fit_image_get_node(
								fit_hdr,
								fit_uname_fdt);
1581 1582
				}
				if (fdt_noffset < 0) {
S
Stephen Warren 已提交
1583 1584
					fdt_error("Could not find subimage "
							"node\n");
1585 1586 1587
					goto error;
				}

S
Stephen Warren 已提交
1588
				printf("   Trying '%s' FDT blob subimage\n",
1589 1590
						fit_uname_fdt);

S
Stephen Warren 已提交
1591
				if (!fit_check_fdt(fit_hdr, fdt_noffset,
1592 1593 1594 1595
							images->verify))
					goto error;

				/* get ramdisk image data address and length */
S
Stephen Warren 已提交
1596
				if (fit_image_get_data(fit_hdr, fdt_noffset,
1597
							&data, &size)) {
S
Stephen Warren 已提交
1598 1599
					fdt_error("Could not find FDT "
							"subimage data");
1600 1601 1602 1603
					goto error;
				}

				/* verift that image data is a proper FDT blob */
S
Stephen Warren 已提交
1604 1605
				if (fdt_check_header((char *)data) != 0) {
					fdt_error("Subimage data is not a FTD");
1606 1607 1608 1609 1610 1611 1612 1613
					goto error;
				}

				/*
				 * move image data to the load address,
				 * make sure we don't overwrite initial image
				 */
				image_start = (ulong)fit_hdr;
S
Stephen Warren 已提交
1614
				image_end = fit_get_end(fit_hdr);
1615

S
Stephen Warren 已提交
1616
				if (fit_image_get_load(fit_hdr, fdt_noffset,
1617 1618 1619 1620 1621
							&load_start) == 0) {
					load_end = load_start + size;

					if ((load_start < image_end) &&
							(load_end > image_start)) {
S
Stephen Warren 已提交
1622
						fdt_error("FDT overwritten");
1623 1624 1625
						goto error;
					}

S
Stephen Warren 已提交
1626 1627 1628 1629
					printf("   Loading FDT from 0x%08lx "
							"to 0x%08lx\n",
							(ulong)data,
							load_start);
1630

S
Stephen Warren 已提交
1631
					memmove((void *)load_start,
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
							(void *)data, size);

					fdt_blob = (char *)load_start;
				} else {
					fdt_blob = (char *)data;
				}

				images->fit_hdr_fdt = fit_hdr;
				images->fit_uname_fdt = fit_uname_fdt;
				images->fit_noffset_fdt = fdt_noffset;
				break;
			} else
#endif
			{
				/*
				 * FDT blob
				 */
				fdt_blob = (char *)fdt_addr;
S
Stephen Warren 已提交
1650 1651 1652
				debug("*  fdt: raw FDT blob\n");
				printf("## Flattened Device Tree blob at "
					"%08lx\n", (long)fdt_blob);
1653 1654 1655
			}
			break;
		default:
S
Stephen Warren 已提交
1656 1657
			puts("ERROR: Did not find a cmdline Flattened Device "
				"Tree\n");
1658 1659 1660
			goto error;
		}

S
Simon Glass 已提交
1661
		printf("   Booting using the fdt blob at 0x%p\n", fdt_blob);
1662 1663

	} else if (images->legacy_hdr_valid &&
S
Stephen Warren 已提交
1664 1665
			image_check_type(&images->legacy_hdr_os_copy,
						IH_TYPE_MULTI)) {
1666 1667 1668 1669 1670 1671 1672

		ulong fdt_data, fdt_len;

		/*
		 * Now check if we have a legacy multi-component image,
		 * get second entry data start address and len.
		 */
S
Stephen Warren 已提交
1673
		printf("## Flattened Device Tree from multi "
1674 1675 1676
			"component Image at %08lX\n",
			(ulong)images->legacy_hdr_os);

S
Stephen Warren 已提交
1677 1678
		image_multi_getimg(images->legacy_hdr_os, 2, &fdt_data,
					&fdt_len);
1679 1680 1681
		if (fdt_len) {

			fdt_blob = (char *)fdt_data;
S
Simon Glass 已提交
1682
			printf("   Booting using the fdt at 0x%p\n", fdt_blob);
1683

S
Stephen Warren 已提交
1684 1685
			if (fdt_check_header(fdt_blob) != 0) {
				fdt_error("image is not a fdt");
1686 1687 1688
				goto error;
			}

1689
			if (fdt_totalsize(fdt_blob) != fdt_len) {
S
Stephen Warren 已提交
1690
				fdt_error("fdt size != image size");
1691 1692 1693
				goto error;
			}
		} else {
S
Stephen Warren 已提交
1694
			debug("## No Flattened Device Tree\n");
1695 1696 1697
			return 0;
		}
	} else {
S
Stephen Warren 已提交
1698
		debug("## No Flattened Device Tree\n");
1699 1700 1701 1702
		return 0;
	}

	*of_flat_tree = fdt_blob;
1703
	*of_size = fdt_totalsize(fdt_blob);
S
Stephen Warren 已提交
1704
	debug("   of_flat_tree at 0x%08lx size 0x%08lx\n",
1705
			(ulong)*of_flat_tree, *of_size);
1706 1707 1708 1709

	return 0;

error:
K
Kim Phillips 已提交
1710
	*of_flat_tree = NULL;
1711 1712 1713 1714 1715
	*of_size = 0;
	return 1;
}
#endif /* CONFIG_OF_LIBFDT */

1716
#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
1717
/**
1718
 * boot_get_cmdline - allocate and initialize kernel cmdline
1719
 * @lmb: pointer to lmb handle, will be used for memory mgmt
1720 1721 1722
 * @cmd_start: pointer to a ulong variable, will hold cmdline start
 * @cmd_end: pointer to a ulong variable, will hold cmdline end
 *
1723
 * boot_get_cmdline() allocates space for kernel command line below
1724
 * BOOTMAPSZ + getenv_bootm_low() address. If "bootargs" U-boot environemnt
1725 1726 1727 1728
 * variable is present its contents is copied to allocated kernel
 * command line.
 *
 * returns:
1729 1730
 *      0 - success
 *     -1 - failure
1731
 */
S
Stephen Warren 已提交
1732
int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
1733 1734 1735 1736
{
	char *cmdline;
	char *s;

1737
	cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
1738
				getenv_bootm_mapsize() + getenv_bootm_low());
1739 1740 1741

	if (cmdline == NULL)
		return -1;
1742 1743 1744 1745 1746 1747 1748 1749 1750

	if ((s = getenv("bootargs")) == NULL)
		s = "";

	strcpy(cmdline, s);

	*cmd_start = (ulong) & cmdline[0];
	*cmd_end = *cmd_start + strlen(cmdline);

S
Stephen Warren 已提交
1751
	debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
1752

1753
	return 0;
1754
}
1755
#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
1756

1757
#ifdef CONFIG_SYS_BOOT_GET_KBD
1758
/**
1759
 * boot_get_kbd - allocate and initialize kernel copy of board info
1760
 * @lmb: pointer to lmb handle, will be used for memory mgmt
1761 1762
 * @kbd: double pointer to board info data
 *
1763
 * boot_get_kbd() allocates space for kernel copy of board info data below
1764 1765
 * BOOTMAPSZ + getenv_bootm_low() address and kernel board info is initialized
 * with the current u-boot board info data.
1766 1767
 *
 * returns:
1768 1769
 *      0 - success
 *     -1 - failure
1770
 */
S
Stephen Warren 已提交
1771
int boot_get_kbd(struct lmb *lmb, bd_t **kbd)
1772
{
1773
	*kbd = (bd_t *)(ulong)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
1774
				getenv_bootm_mapsize() + getenv_bootm_low());
1775 1776 1777
	if (*kbd == NULL)
		return -1;

1778 1779
	**kbd = *(gd->bd);

S
Stephen Warren 已提交
1780
	debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
1781 1782 1783 1784 1785

#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
	do_bdinfo(NULL, 0, 0, NULL);
#endif

1786
	return 0;
1787
}
1788
#endif /* CONFIG_SYS_BOOT_GET_KBD */
1789
#endif /* !USE_HOSTCC */