xref: /OK3568_Linux_fs/u-boot/cmd/booti.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2000-2009
3*4882a593Smuzhiyun  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <bootm.h>
10*4882a593Smuzhiyun #include <command.h>
11*4882a593Smuzhiyun #include <image.h>
12*4882a593Smuzhiyun #include <lmb.h>
13*4882a593Smuzhiyun #include <mapmem.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/sizes.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /* See Documentation/arm64/booting.txt in the Linux kernel */
20*4882a593Smuzhiyun struct Image_header {
21*4882a593Smuzhiyun 	uint32_t	code0;		/* Executable code */
22*4882a593Smuzhiyun 	uint32_t	code1;		/* Executable code */
23*4882a593Smuzhiyun 	uint64_t	text_offset;	/* Image load offset, LE */
24*4882a593Smuzhiyun 	uint64_t	image_size;	/* Effective Image size, LE */
25*4882a593Smuzhiyun 	uint64_t	flags;		/* Kernel flags, LE */
26*4882a593Smuzhiyun 	uint64_t	res2;		/* reserved */
27*4882a593Smuzhiyun 	uint64_t	res3;		/* reserved */
28*4882a593Smuzhiyun 	uint64_t	res4;		/* reserved */
29*4882a593Smuzhiyun 	uint32_t	magic;		/* Magic number */
30*4882a593Smuzhiyun 	uint32_t	res5;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define LINUX_ARM64_IMAGE_MAGIC	0x644d5241
34*4882a593Smuzhiyun 
booti_setup(bootm_headers_t * images)35*4882a593Smuzhiyun static int booti_setup(bootm_headers_t *images)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct Image_header *ih;
38*4882a593Smuzhiyun 	uint64_t dst;
39*4882a593Smuzhiyun 	uint64_t image_size, text_offset;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	ih = (struct Image_header *)map_sysmem(images->ep, 0);
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
44*4882a593Smuzhiyun 		puts("Bad Linux ARM64 Image magic!\n");
45*4882a593Smuzhiyun 		return 1;
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	/*
49*4882a593Smuzhiyun 	 * Prior to Linux commit a2c1d73b94ed, the text_offset field
50*4882a593Smuzhiyun 	 * is of unknown endianness.  In these cases, the image_size
51*4882a593Smuzhiyun 	 * field is zero, and we can assume a fixed value of 0x80000.
52*4882a593Smuzhiyun 	 */
53*4882a593Smuzhiyun 	if (ih->image_size == 0) {
54*4882a593Smuzhiyun 		puts("Image lacks image_size field, assuming 16MiB\n");
55*4882a593Smuzhiyun 		image_size = 16 << 20;
56*4882a593Smuzhiyun 		text_offset = 0x80000;
57*4882a593Smuzhiyun 	} else {
58*4882a593Smuzhiyun 		image_size = le64_to_cpu(ih->image_size);
59*4882a593Smuzhiyun 		text_offset = le64_to_cpu(ih->text_offset);
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	/*
63*4882a593Smuzhiyun 	 * If bit 3 of the flags field is set, the 2MB aligned base of the
64*4882a593Smuzhiyun 	 * kernel image can be anywhere in physical memory, so respect
65*4882a593Smuzhiyun 	 * images->ep.  Otherwise, relocate the image to the base of RAM
66*4882a593Smuzhiyun 	 * since memory below it is not accessible via the linear mapping.
67*4882a593Smuzhiyun 	 */
68*4882a593Smuzhiyun 	if (le64_to_cpu(ih->flags) & BIT(3))
69*4882a593Smuzhiyun 		dst = images->ep - text_offset;
70*4882a593Smuzhiyun 	else
71*4882a593Smuzhiyun 		dst = gd->bd->bi_dram[0].start;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	dst = ALIGN(dst, SZ_2M) + text_offset;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	unmap_sysmem(ih);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (images->ep != dst) {
78*4882a593Smuzhiyun 		void *src;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 		debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 		src = (void *)images->ep;
83*4882a593Smuzhiyun 		images->ep = dst;
84*4882a593Smuzhiyun 		memmove((void *)dst, src, image_size);
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	return 0;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /*
91*4882a593Smuzhiyun  * Image booting support
92*4882a593Smuzhiyun  */
booti_start(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[],bootm_headers_t * images)93*4882a593Smuzhiyun static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
94*4882a593Smuzhiyun 			char * const argv[], bootm_headers_t *images)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	int ret;
97*4882a593Smuzhiyun 	struct Image_header *ih;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
100*4882a593Smuzhiyun 			      images, 1);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* Setup Linux kernel Image entry point */
103*4882a593Smuzhiyun 	if (!argc) {
104*4882a593Smuzhiyun 		images->ep = load_addr;
105*4882a593Smuzhiyun 		debug("*  kernel: default image load address = 0x%08lx\n",
106*4882a593Smuzhiyun 				load_addr);
107*4882a593Smuzhiyun 	} else {
108*4882a593Smuzhiyun 		images->ep = simple_strtoul(argv[0], NULL, 16);
109*4882a593Smuzhiyun 		debug("*  kernel: cmdline image address = 0x%08lx\n",
110*4882a593Smuzhiyun 			images->ep);
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	ret = booti_setup(images);
114*4882a593Smuzhiyun 	if (ret != 0)
115*4882a593Smuzhiyun 		return 1;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	ih = (struct Image_header *)map_sysmem(images->ep, 0);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	unmap_sysmem(ih);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	/*
124*4882a593Smuzhiyun 	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
125*4882a593Smuzhiyun 	 * have a header that provide this informaiton.
126*4882a593Smuzhiyun 	 */
127*4882a593Smuzhiyun 	if (bootm_find_images(flag, argc, argv))
128*4882a593Smuzhiyun 		return 1;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	return 0;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
do_booti(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])133*4882a593Smuzhiyun int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	int ret;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/* Consume 'booti' */
138*4882a593Smuzhiyun 	argc--; argv++;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (booti_start(cmdtp, flag, argc, argv, &images))
141*4882a593Smuzhiyun 		return 1;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	/*
144*4882a593Smuzhiyun 	 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
145*4882a593Smuzhiyun 	 * disable interrupts ourselves
146*4882a593Smuzhiyun 	 */
147*4882a593Smuzhiyun 	bootm_disable_interrupts();
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	images.os.os = IH_OS_LINUX;
150*4882a593Smuzhiyun 	images.os.arch = IH_ARCH_ARM64;
151*4882a593Smuzhiyun 	ret = do_bootm_states(cmdtp, flag, argc, argv,
152*4882a593Smuzhiyun #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
153*4882a593Smuzhiyun 			      BOOTM_STATE_RAMDISK |
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun 			      BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
156*4882a593Smuzhiyun 			      BOOTM_STATE_OS_GO,
157*4882a593Smuzhiyun 			      &images, 1);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	return ret;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun #ifdef CONFIG_SYS_LONGHELP
163*4882a593Smuzhiyun static char booti_help_text[] =
164*4882a593Smuzhiyun 	"[addr [initrd[:size]] [fdt]]\n"
165*4882a593Smuzhiyun 	"    - boot arm64 Linux Image stored in memory\n"
166*4882a593Smuzhiyun 	"\tThe argument 'initrd' is optional and specifies the address\n"
167*4882a593Smuzhiyun 	"\tof an initrd in memory. The optional parameter ':size' allows\n"
168*4882a593Smuzhiyun 	"\tspecifying the size of a RAW initrd.\n"
169*4882a593Smuzhiyun #if defined(CONFIG_OF_LIBFDT)
170*4882a593Smuzhiyun 	"\tSince booting a Linux kernel requires a flat device-tree, a\n"
171*4882a593Smuzhiyun 	"\tthird argument providing the address of the device-tree blob\n"
172*4882a593Smuzhiyun 	"\tis required. To boot a kernel with a device-tree blob but\n"
173*4882a593Smuzhiyun 	"\twithout an initrd image, use a '-' for the initrd argument.\n"
174*4882a593Smuzhiyun #endif
175*4882a593Smuzhiyun 	"";
176*4882a593Smuzhiyun #endif
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun U_BOOT_CMD(
179*4882a593Smuzhiyun 	booti,	CONFIG_SYS_MAXARGS,	1,	do_booti,
180*4882a593Smuzhiyun 	"boot arm64 Linux Image image from memory", booti_help_text
181*4882a593Smuzhiyun );
182