xref: /rk3399_rockchip-uboot/cmd/booti.c (revision 2808576491ae36b6ea96743005058f370d936beb)
15db28905STom Rini /*
25db28905STom Rini  * (C) Copyright 2000-2009
35db28905STom Rini  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
45db28905STom Rini  *
55db28905STom Rini  * SPDX-License-Identifier:	GPL-2.0+
65db28905STom Rini  */
75db28905STom Rini 
85db28905STom Rini #include <common.h>
95db28905STom Rini #include <bootm.h>
105db28905STom Rini #include <command.h>
115db28905STom Rini #include <image.h>
125db28905STom Rini #include <lmb.h>
135db28905STom Rini #include <mapmem.h>
14*28085764SMasahiro Yamada #include <linux/kernel.h>
15*28085764SMasahiro Yamada #include <linux/sizes.h>
165db28905STom Rini 
175db28905STom Rini DECLARE_GLOBAL_DATA_PTR;
185db28905STom Rini 
195db28905STom Rini /* See Documentation/arm64/booting.txt in the Linux kernel */
205db28905STom Rini struct Image_header {
215db28905STom Rini 	uint32_t	code0;		/* Executable code */
225db28905STom Rini 	uint32_t	code1;		/* Executable code */
235db28905STom Rini 	uint64_t	text_offset;	/* Image load offset, LE */
245db28905STom Rini 	uint64_t	image_size;	/* Effective Image size, LE */
25*28085764SMasahiro Yamada 	uint64_t	flags;		/* Kernel flags, LE */
265db28905STom Rini 	uint64_t	res2;		/* reserved */
275db28905STom Rini 	uint64_t	res3;		/* reserved */
285db28905STom Rini 	uint64_t	res4;		/* reserved */
295db28905STom Rini 	uint32_t	magic;		/* Magic number */
305db28905STom Rini 	uint32_t	res5;
315db28905STom Rini };
325db28905STom Rini 
335db28905STom Rini #define LINUX_ARM64_IMAGE_MAGIC	0x644d5241
345db28905STom Rini 
booti_setup(bootm_headers_t * images)355db28905STom Rini static int booti_setup(bootm_headers_t *images)
365db28905STom Rini {
375db28905STom Rini 	struct Image_header *ih;
385db28905STom Rini 	uint64_t dst;
39*28085764SMasahiro Yamada 	uint64_t image_size, text_offset;
405db28905STom Rini 
415db28905STom Rini 	ih = (struct Image_header *)map_sysmem(images->ep, 0);
425db28905STom Rini 
435db28905STom Rini 	if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
445db28905STom Rini 		puts("Bad Linux ARM64 Image magic!\n");
455db28905STom Rini 		return 1;
465db28905STom Rini 	}
475db28905STom Rini 
48*28085764SMasahiro Yamada 	/*
49*28085764SMasahiro Yamada 	 * Prior to Linux commit a2c1d73b94ed, the text_offset field
50*28085764SMasahiro Yamada 	 * is of unknown endianness.  In these cases, the image_size
51*28085764SMasahiro Yamada 	 * field is zero, and we can assume a fixed value of 0x80000.
52*28085764SMasahiro Yamada 	 */
535db28905STom Rini 	if (ih->image_size == 0) {
545db28905STom Rini 		puts("Image lacks image_size field, assuming 16MiB\n");
555db28905STom Rini 		image_size = 16 << 20;
56*28085764SMasahiro Yamada 		text_offset = 0x80000;
575db28905STom Rini 	} else {
585db28905STom Rini 		image_size = le64_to_cpu(ih->image_size);
59*28085764SMasahiro Yamada 		text_offset = le64_to_cpu(ih->text_offset);
605db28905STom Rini 	}
615db28905STom Rini 
625db28905STom Rini 	/*
63*28085764SMasahiro Yamada 	 * If bit 3 of the flags field is set, the 2MB aligned base of the
64*28085764SMasahiro Yamada 	 * kernel image can be anywhere in physical memory, so respect
65*28085764SMasahiro Yamada 	 * images->ep.  Otherwise, relocate the image to the base of RAM
66*28085764SMasahiro Yamada 	 * since memory below it is not accessible via the linear mapping.
675db28905STom Rini 	 */
68*28085764SMasahiro Yamada 	if (le64_to_cpu(ih->flags) & BIT(3))
69*28085764SMasahiro Yamada 		dst = images->ep - text_offset;
70*28085764SMasahiro Yamada 	else
71*28085764SMasahiro Yamada 		dst = gd->bd->bi_dram[0].start;
72*28085764SMasahiro Yamada 
73*28085764SMasahiro Yamada 	dst = ALIGN(dst, SZ_2M) + text_offset;
745db28905STom Rini 
755db28905STom Rini 	unmap_sysmem(ih);
765db28905STom Rini 
775db28905STom Rini 	if (images->ep != dst) {
785db28905STom Rini 		void *src;
795db28905STom Rini 
805db28905STom Rini 		debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
815db28905STom Rini 
825db28905STom Rini 		src = (void *)images->ep;
835db28905STom Rini 		images->ep = dst;
845db28905STom Rini 		memmove((void *)dst, src, image_size);
855db28905STom Rini 	}
865db28905STom Rini 
875db28905STom Rini 	return 0;
885db28905STom Rini }
895db28905STom Rini 
905db28905STom Rini /*
915db28905STom Rini  * Image booting support
925db28905STom Rini  */
booti_start(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],bootm_headers_t * images)935db28905STom Rini static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
945db28905STom Rini 			char * const argv[], bootm_headers_t *images)
955db28905STom Rini {
965db28905STom Rini 	int ret;
975db28905STom Rini 	struct Image_header *ih;
985db28905STom Rini 
995db28905STom Rini 	ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
1005db28905STom Rini 			      images, 1);
1015db28905STom Rini 
1025db28905STom Rini 	/* Setup Linux kernel Image entry point */
1035db28905STom Rini 	if (!argc) {
1045db28905STom Rini 		images->ep = load_addr;
1055db28905STom Rini 		debug("*  kernel: default image load address = 0x%08lx\n",
1065db28905STom Rini 				load_addr);
1075db28905STom Rini 	} else {
1085db28905STom Rini 		images->ep = simple_strtoul(argv[0], NULL, 16);
1095db28905STom Rini 		debug("*  kernel: cmdline image address = 0x%08lx\n",
1105db28905STom Rini 			images->ep);
1115db28905STom Rini 	}
1125db28905STom Rini 
1135db28905STom Rini 	ret = booti_setup(images);
1145db28905STom Rini 	if (ret != 0)
1155db28905STom Rini 		return 1;
1165db28905STom Rini 
1175db28905STom Rini 	ih = (struct Image_header *)map_sysmem(images->ep, 0);
1185db28905STom Rini 
1195db28905STom Rini 	lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
1205db28905STom Rini 
1215db28905STom Rini 	unmap_sysmem(ih);
1225db28905STom Rini 
1235db28905STom Rini 	/*
1245db28905STom Rini 	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
1255db28905STom Rini 	 * have a header that provide this informaiton.
1265db28905STom Rini 	 */
1275db28905STom Rini 	if (bootm_find_images(flag, argc, argv))
1285db28905STom Rini 		return 1;
1295db28905STom Rini 
1305db28905STom Rini 	return 0;
1315db28905STom Rini }
1325db28905STom Rini 
do_booti(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1335db28905STom Rini int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1345db28905STom Rini {
1355db28905STom Rini 	int ret;
1365db28905STom Rini 
1375db28905STom Rini 	/* Consume 'booti' */
1385db28905STom Rini 	argc--; argv++;
1395db28905STom Rini 
1405db28905STom Rini 	if (booti_start(cmdtp, flag, argc, argv, &images))
1415db28905STom Rini 		return 1;
1425db28905STom Rini 
1435db28905STom Rini 	/*
1445db28905STom Rini 	 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
1455db28905STom Rini 	 * disable interrupts ourselves
1465db28905STom Rini 	 */
1475db28905STom Rini 	bootm_disable_interrupts();
1485db28905STom Rini 
1495db28905STom Rini 	images.os.os = IH_OS_LINUX;
1500fff19a6SScott Wood 	images.os.arch = IH_ARCH_ARM64;
1515db28905STom Rini 	ret = do_bootm_states(cmdtp, flag, argc, argv,
1524943dc2fSCédric Schieli #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
1534943dc2fSCédric Schieli 			      BOOTM_STATE_RAMDISK |
1544943dc2fSCédric Schieli #endif
1555db28905STom Rini 			      BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1565db28905STom Rini 			      BOOTM_STATE_OS_GO,
1575db28905STom Rini 			      &images, 1);
1585db28905STom Rini 
1595db28905STom Rini 	return ret;
1605db28905STom Rini }
1615db28905STom Rini 
1625db28905STom Rini #ifdef CONFIG_SYS_LONGHELP
1635db28905STom Rini static char booti_help_text[] =
1645db28905STom Rini 	"[addr [initrd[:size]] [fdt]]\n"
1655db28905STom Rini 	"    - boot arm64 Linux Image stored in memory\n"
1665db28905STom Rini 	"\tThe argument 'initrd' is optional and specifies the address\n"
1675db28905STom Rini 	"\tof an initrd in memory. The optional parameter ':size' allows\n"
1685db28905STom Rini 	"\tspecifying the size of a RAW initrd.\n"
1695db28905STom Rini #if defined(CONFIG_OF_LIBFDT)
1705db28905STom Rini 	"\tSince booting a Linux kernel requires a flat device-tree, a\n"
1715db28905STom Rini 	"\tthird argument providing the address of the device-tree blob\n"
1725db28905STom Rini 	"\tis required. To boot a kernel with a device-tree blob but\n"
1735db28905STom Rini 	"\twithout an initrd image, use a '-' for the initrd argument.\n"
1745db28905STom Rini #endif
1755db28905STom Rini 	"";
1765db28905STom Rini #endif
1775db28905STom Rini 
1785db28905STom Rini U_BOOT_CMD(
1795db28905STom Rini 	booti,	CONFIG_SYS_MAXARGS,	1,	do_booti,
1805db28905STom Rini 	"boot arm64 Linux Image image from memory", booti_help_text
1815db28905STom Rini );
182