1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 */
6
7#include <common.h>
8#include <bootm.h>
9#include <command.h>
10#include <image.h>
11#include <irq_func.h>
12#include <lmb.h>
13#include <log.h>
14#include <mapmem.h>
15#include <asm/global_data.h>
16#include <linux/kernel.h>
17#include <linux/sizes.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20/*
21 * Image booting support
22 */
23static int booti_start(struct bootm_info *bmi)
24{
25	struct bootm_headers *images = bmi->images;
26	int ret;
27	ulong ld;
28	ulong relocated_addr;
29	ulong image_size;
30	uint8_t *temp;
31	ulong dest;
32	ulong dest_end;
33	unsigned long comp_len;
34	unsigned long decomp_len;
35	int ctype;
36
37	ret = bootm_run_states(bmi, BOOTM_STATE_START);
38
39	/* Setup Linux kernel Image entry point */
40	if (!bmi->addr_img) {
41		ld = image_load_addr;
42		debug("*  kernel: default image load address = 0x%08lx\n",
43				image_load_addr);
44	} else {
45		ld = hextoul(bmi->addr_img, NULL);
46		debug("*  kernel: cmdline image address = 0x%08lx\n", ld);
47	}
48
49	temp = map_sysmem(ld, 0);
50	ctype = image_decomp_type(temp, 2);
51	if (ctype > 0) {
52		dest = env_get_ulong("kernel_comp_addr_r", 16, 0);
53		comp_len = env_get_ulong("kernel_comp_size", 16, 0);
54		if (!dest || !comp_len) {
55			puts("kernel_comp_addr_r or kernel_comp_size is not provided!\n");
56			return -EINVAL;
57		}
58		if (dest < gd->ram_base || dest > gd->ram_top) {
59			puts("kernel_comp_addr_r is outside of DRAM range!\n");
60			return -EINVAL;
61		}
62
63		debug("kernel image compression type %d size = 0x%08lx address = 0x%08lx\n",
64			ctype, comp_len, (ulong)dest);
65		decomp_len = comp_len * 10;
66		ret = image_decomp(ctype, 0, ld, IH_TYPE_KERNEL,
67				 (void *)dest, (void *)ld, comp_len,
68				 decomp_len, &dest_end);
69		if (ret)
70			return ret;
71		/* dest_end contains the uncompressed Image size */
72		memmove((void *) ld, (void *)dest, dest_end);
73	}
74	unmap_sysmem((void *)ld);
75
76	ret = booti_setup(ld, &relocated_addr, &image_size, false);
77	if (ret)
78		return 1;
79
80	/* Handle BOOTM_STATE_LOADOS */
81	if (relocated_addr != ld) {
82		printf("Moving Image from 0x%lx to 0x%lx, end=%lx\n", ld,
83		       relocated_addr, relocated_addr + image_size);
84		memmove((void *)relocated_addr, (void *)ld, image_size);
85	}
86
87	images->ep = relocated_addr;
88	images->os.start = relocated_addr;
89	images->os.end = relocated_addr + image_size;
90
91	lmb_reserve(&images->lmb, images->ep, le32_to_cpu(image_size));
92
93	/*
94	 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
95	 * have a header that provide this informaiton.
96	 */
97	if (bootm_find_images(image_load_addr, bmi->conf_ramdisk, bmi->conf_fdt,
98			      relocated_addr, image_size))
99		return 1;
100
101	return 0;
102}
103
104int do_booti(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
105{
106	struct bootm_info bmi;
107	int states;
108	int ret;
109
110	/* Consume 'booti' */
111	argc--; argv++;
112
113	bootm_init(&bmi);
114	if (argc)
115		bmi.addr_img = argv[0];
116	if (argc > 1)
117		bmi.conf_ramdisk = argv[1];
118	if (argc > 2)
119		bmi.conf_fdt = argv[2];
120	bmi.boot_progress = true;
121	bmi.cmd_name = "booti";
122	/* do not set up argc and argv[] since nothing uses them */
123
124	if (booti_start(&bmi))
125		return 1;
126
127	/*
128	 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
129	 * disable interrupts ourselves
130	 */
131	bootm_disable_interrupts();
132
133	images.os.os = IH_OS_LINUX;
134	if (IS_ENABLED(CONFIG_RISCV_SMODE))
135		images.os.arch = IH_ARCH_RISCV;
136	else if (IS_ENABLED(CONFIG_ARM64))
137		images.os.arch = IH_ARCH_ARM64;
138
139	states = BOOTM_STATE_MEASURE | BOOTM_STATE_OS_PREP |
140		BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO;
141	if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
142		states |= BOOTM_STATE_RAMDISK;
143
144	ret = bootm_run_states(&bmi, states);
145
146	return ret;
147}
148
149U_BOOT_LONGHELP(booti,
150	"[addr [initrd[:size]] [fdt]]\n"
151	"    - boot Linux flat or compressed 'Image' stored at 'addr'\n"
152	"\tThe argument 'initrd' is optional and specifies the address\n"
153	"\tof an initrd in memory. The optional parameter ':size' allows\n"
154	"\tspecifying the size of a RAW initrd.\n"
155	"\tCurrently only booting from gz, bz2, lzma and lz4 compression\n"
156	"\ttypes are supported. In order to boot from any of these compressed\n"
157	"\timages, user have to set kernel_comp_addr_r and kernel_comp_size environment\n"
158	"\tvariables beforehand.\n"
159#if defined(CONFIG_OF_LIBFDT)
160	"\tSince booting a Linux kernel requires a flat device-tree, a\n"
161	"\tthird argument providing the address of the device-tree blob\n"
162	"\tis required. To boot a kernel with a device-tree blob but\n"
163	"\twithout an initrd image, use a '-' for the initrd argument.\n"
164#endif
165	);
166
167U_BOOT_CMD(
168	booti,	CONFIG_SYS_MAXARGS,	1,	do_booti,
169	"boot Linux kernel 'Image' format from memory", booti_help_text
170);
171