1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002
4  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
5  * Marius Groeger <mgroeger@sysgo.de>
6  *
7  * Copyright (C) 2001  Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
8  */
9 
10 #include <bootm.h>
11 #include <bootstage.h>
12 #include <command.h>
13 #include <efi.h>
14 #include <hang.h>
15 #include <log.h>
16 #include <asm/global_data.h>
17 #include <dm/device.h>
18 #include <dm/root.h>
19 #include <errno.h>
20 #include <fdt_support.h>
21 #include <image.h>
22 #include <u-boot/zlib.h>
23 #include <asm/bootparam.h>
24 #include <asm/cpu.h>
25 #include <asm/byteorder.h>
26 #include <asm/zimage.h>
27 #ifdef CONFIG_SYS_COREBOOT
28 #include <asm/arch/timestamp.h>
29 #endif
30 
31 DECLARE_GLOBAL_DATA_PTR;
32 
33 #define COMMAND_LINE_OFFSET 0x9000
34 
bootm_announce_and_cleanup(void)35 void bootm_announce_and_cleanup(void)
36 {
37 	printf("\nStarting kernel ...\n\n");
38 
39 #ifdef CONFIG_SYS_COREBOOT
40 	timestamp_add_now(TS_START_KERNEL);
41 #endif
42 	bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
43 #if IS_ENABLED(CONFIG_BOOTSTAGE_REPORT)
44 	bootstage_report();
45 #endif
46 
47 	/*
48 	 * Call remove function of all devices with a removal flag set.
49 	 * This may be useful for last-stage operations, like cancelling
50 	 * of DMA operation or releasing device internal buffers.
51 	 */
52 	dm_remove_devices_active();
53 }
54 
55 #if defined(CONFIG_OF_LIBFDT) && !defined(CONFIG_OF_NO_KERNEL)
arch_fixup_memory_node(void * blob)56 int arch_fixup_memory_node(void *blob)
57 {
58 	struct bd_info	*bd = gd->bd;
59 	int bank;
60 	u64 start[CONFIG_NR_DRAM_BANKS];
61 	u64 size[CONFIG_NR_DRAM_BANKS];
62 
63 	for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
64 		start[bank] = bd->bi_dram[bank].start;
65 		size[bank] = bd->bi_dram[bank].size;
66 	}
67 
68 	return fdt_fixup_memory_banks(blob, start, size, CONFIG_NR_DRAM_BANKS);
69 }
70 #endif
71 
72 /* Subcommand: PREP */
boot_prep_linux(struct bootm_headers * images)73 static int boot_prep_linux(struct bootm_headers *images)
74 {
75 	char *cmd_line_dest = NULL;
76 	struct legacy_img_hdr *hdr;
77 	int is_zimage = 0;
78 	void *data = NULL;
79 	size_t len;
80 	int ret;
81 
82 	if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) {
83 		debug("using: FDT\n");
84 		if (image_setup_linux(images)) {
85 			puts("FDT creation failed! hanging...");
86 			hang();
87 		}
88 	}
89 
90 	if (images->legacy_hdr_valid) {
91 		hdr = images->legacy_hdr_os;
92 		if (image_check_type(hdr, IH_TYPE_MULTI)) {
93 			ulong os_data, os_len;
94 
95 			/* if multi-part image, we need to get first subimage */
96 			image_multi_getimg(hdr, 0, &os_data, &os_len);
97 			data = (void *)os_data;
98 			len = os_len;
99 		} else {
100 			/* otherwise get image data */
101 			data = (void *)image_get_data(hdr);
102 			len = image_get_data_size(hdr);
103 		}
104 		is_zimage = 1;
105 #if defined(CONFIG_FIT)
106 	} else if (images->fit_uname_os && is_zimage) {
107 		ret = fit_image_get_data(images->fit_hdr_os,
108 					 images->fit_noffset_os,
109 					 (const void **)&data, &len);
110 		if (ret) {
111 			puts("Can't get image data/size!\n");
112 			goto error;
113 		}
114 		is_zimage = 1;
115 #endif
116 	}
117 
118 	if (is_zimage) {
119 		ulong load_address;
120 		char *base_ptr;
121 
122 		base_ptr = (char *)load_zimage(data, len, &load_address);
123 		if (!base_ptr) {
124 			puts("## Kernel loading failed ...\n");
125 			goto error;
126 		}
127 		images->os.load = load_address;
128 		cmd_line_dest = base_ptr + COMMAND_LINE_OFFSET;
129 		images->ep = (ulong)base_ptr;
130 	} else if (images->ep) {
131 		cmd_line_dest = (void *)images->ep + COMMAND_LINE_OFFSET;
132 	} else {
133 		printf("## Kernel loading failed (missing x86 kernel setup) ...\n");
134 		goto error;
135 	}
136 
137 	printf("Setup at %#08lx\n", images->ep);
138 	ret = setup_zimage((void *)images->ep, cmd_line_dest,
139 			0, images->rd_start,
140 			images->rd_end - images->rd_start, 0);
141 
142 	if (ret) {
143 		printf("## Setting up boot parameters failed ...\n");
144 		return 1;
145 	}
146 
147 	return 0;
148 
149 error:
150 	return 1;
151 }
152 
boot_linux_kernel(ulong setup_base,ulong entry,bool image_64bit)153 int boot_linux_kernel(ulong setup_base, ulong entry, bool image_64bit)
154 {
155 	bootm_announce_and_cleanup();
156 
157 #ifdef CONFIG_SYS_COREBOOT
158 	timestamp_add_now(TS_U_BOOT_START_KERNEL);
159 #endif
160 
161 	/*
162 	 * Exit EFI boot services just before jumping, after all console
163 	 * output, since the console won't be available afterwards.
164 	 */
165 	if (IS_ENABLED(CONFIG_EFI_APP)) {
166 		int ret;
167 
168 		ret = efi_store_memory_map(efi_get_priv());
169 		if (ret)
170 			return ret;
171 		printf("Exiting EFI boot services\n");
172 		ret = efi_call_exit_boot_services();
173 		if (ret)
174 			return ret;
175 	}
176 
177 	if (image_64bit) {
178 		if (!cpu_has_64bit()) {
179 			puts("Cannot boot 64-bit kernel on 32-bit machine\n");
180 			return -EFAULT;
181 		}
182 		/*
183 		 * At present 64-bit U-Boot only supports booting a 64-bit
184 		 * kernel.
185 		 *
186 		 * TODO(sjg@chromium.org): Support booting 32-bit kernels from
187 		 * 64-bit U-Boot
188 		 */
189 		if (CONFIG_IS_ENABLED(X86_64)) {
190 			typedef void (*h_func)(ulong zero, ulong setup);
191 			h_func func;
192 			struct setup_header *hdr = &(((struct boot_params *)(setup_base))->hdr);
193 
194 			/* Handle kernel with legacy 64-bit entry point at 0x200 */
195 			if (hdr->xloadflags & XLF_KERNEL_64) {
196 				entry += 0x200;
197 			}
198 
199 			/* jump to Linux with rdi=0, rsi=setup_base */
200 			func = (h_func)entry;
201 			func(0, setup_base);
202 		} else {
203 			return cpu_jump_to_64bit(setup_base, entry);
204 		}
205 	} else {
206 		/*
207 		* Set %ebx, %ebp, and %edi to 0, %esi to point to the
208 		* boot_params structure, and then jump to the kernel. We
209 		* assume that %cs is 0x10, 4GB flat, and read/execute, and
210 		* the data segments are 0x18, 4GB flat, and read/write.
211 		* U-Boot is setting them up that way for itself in
212 		* arch/i386/cpu/cpu.c.
213 		*
214 		* Note: this is incomplete for EFI kernels!
215 		*
216 		* This can boot a kernel while running as an EFI application,
217 		* but if the kernel requires EFI support then that support needs
218 		* to be enabled first (see EFI_LOADER). Also the EFI information
219 		* must enabled with setup_efi_info(). See setup_zimage() for
220 		* how this is done with the stub.
221 		*/
222 		__asm__ __volatile__ (
223 		"movl $0, %%ebp\n"
224 		"cli\n"
225 		"jmp *%[kernel_entry]\n"
226 		:: [kernel_entry]"a"(entry),
227 		[boot_params] "S"(setup_base),
228 		"b"(0), "D"(0)
229 		);
230 	}
231 
232 	/* We can't get to here */
233 	return -EFAULT;
234 }
235 
236 /* Subcommand: GO */
boot_jump_linux(struct bootm_headers * images)237 static int boot_jump_linux(struct bootm_headers *images)
238 {
239 	debug("## Transferring control to Linux (at address %08lx, kernel %08lx) ...\n",
240 	      images->ep, images->os.load);
241 
242 	return boot_linux_kernel(images->ep, images->os.load,
243 				 images->os.arch == IH_ARCH_X86_64);
244 }
245 
do_bootm_linux(int flag,struct bootm_info * bmi)246 int do_bootm_linux(int flag, struct bootm_info *bmi)
247 {
248 	struct bootm_headers *images = bmi->images;
249 
250 	/* No need for those on x86 */
251 	if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
252 		return -1;
253 
254 	if (flag & BOOTM_STATE_OS_PREP)
255 		return boot_prep_linux(images);
256 
257 	if (flag & BOOTM_STATE_OS_GO)
258 		return boot_jump_linux(images);
259 
260 	return boot_jump_linux(images);
261 }
262 
arch_upl_jump(ulong entry,const struct abuf * buf)263 int arch_upl_jump(ulong entry, const struct abuf *buf)
264 {
265 	typedef EFIAPI void (*h_func)(void *hoff);
266 	h_func func;
267 
268 	func = (h_func)(ulong)entry;
269 	func(buf->data);
270 
271 	return -EFAULT;
272 }
273