1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2011
3  * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4  *  - Added prep subcommand support
5  *  - Reorganized source - modeled after powerpc version
6  *
7  * (C) Copyright 2002
8  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9  * Marius Groeger <mgroeger@sysgo.de>
10  *
11  * Copyright (C) 2001  Erik Mouw (J.A.K.Mouw@its.tudelft.nl)
12  */
13 
14 #include <common.h>
15 #include <bootstage.h>
16 #include <command.h>
17 #include <cpu_func.h>
18 #include <dm.h>
19 #include <hang.h>
20 #include <lmb.h>
21 #include <log.h>
22 #include <asm/global_data.h>
23 #include <dm/root.h>
24 #include <env.h>
25 #include <image.h>
26 #include <u-boot/zlib.h>
27 #include <asm/byteorder.h>
28 #include <linux/libfdt.h>
29 #include <mapmem.h>
30 #include <fdt_support.h>
31 #include <asm/bootm.h>
32 #include <asm/secure.h>
33 #include <linux/compiler.h>
34 #include <bootm.h>
35 #include <vxworks.h>
36 #include <asm/cache.h>
37 
38 #ifdef CONFIG_ARMV7_NONSEC
39 #include <asm/armv7.h>
40 #endif
41 #include <asm/setup.h>
42 
43 DECLARE_GLOBAL_DATA_PTR;
44 
45 static struct tag *params;
46 
get_sp(void)47 static ulong get_sp(void)
48 {
49 	ulong ret;
50 
51 	asm("mov %0, sp" : "=r"(ret) : );
52 	return ret;
53 }
54 
arch_lmb_reserve(struct lmb * lmb)55 void arch_lmb_reserve(struct lmb *lmb)
56 {
57 	ulong sp, bank_end;
58 	int bank;
59 
60 	/*
61 	 * Booting a (Linux) kernel image
62 	 *
63 	 * Allocate space for command line and board info - the
64 	 * address should be as high as possible within the reach of
65 	 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
66 	 * memory, which means far enough below the current stack
67 	 * pointer.
68 	 */
69 	sp = get_sp();
70 	debug("## Current stack ends at 0x%08lx ", sp);
71 
72 	/* adjust sp by 4K to be safe */
73 	sp -= 4096;
74 	for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
75 		if (!gd->bd->bi_dram[bank].size ||
76 		    sp < gd->bd->bi_dram[bank].start)
77 			continue;
78 		/* Watch out for RAM at end of address space! */
79 		bank_end = gd->bd->bi_dram[bank].start +
80 			gd->bd->bi_dram[bank].size - 1;
81 		if (sp > bank_end)
82 			continue;
83 		if (bank_end > gd->ram_top)
84 			bank_end = gd->ram_top - 1;
85 
86 		lmb_reserve(lmb, sp, bank_end - sp + 1);
87 		break;
88 	}
89 }
90 
board_quiesce_devices(void)91 __weak void board_quiesce_devices(void)
92 {
93 }
94 
95 /**
96  * announce_and_cleanup() - Print message and prepare for kernel boot
97  *
98  * @fake: non-zero to do everything except actually boot
99  */
announce_and_cleanup(int fake)100 static void announce_and_cleanup(int fake)
101 {
102 	bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
103 #ifdef CONFIG_BOOTSTAGE_FDT
104 	bootstage_fdt_add_report();
105 #endif
106 #ifdef CONFIG_BOOTSTAGE_REPORT
107 	bootstage_report();
108 #endif
109 
110 #ifdef CONFIG_USB_DEVICE
111 	udc_disconnect();
112 #endif
113 
114 	board_quiesce_devices();
115 
116 	printf("\nStarting kernel ...%s\n\n", fake ?
117 		"(fake run for tracing)" : "");
118 	/*
119 	 * Call remove function of all devices with a removal flag set.
120 	 * This may be useful for last-stage operations, like cancelling
121 	 * of DMA operation or releasing device internal buffers.
122 	 */
123 	dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL | DM_REMOVE_NON_VITAL);
124 
125 	/* Remove all active vital devices next */
126 	dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
127 
128 	cleanup_before_linux();
129 }
130 
setup_start_tag(struct bd_info * bd)131 static void setup_start_tag (struct bd_info *bd)
132 {
133 	params = (struct tag *)bd->bi_boot_params;
134 
135 	params->hdr.tag = ATAG_CORE;
136 	params->hdr.size = tag_size (tag_core);
137 
138 	params->u.core.flags = 0;
139 	params->u.core.pagesize = 0;
140 	params->u.core.rootdev = 0;
141 
142 	params = tag_next (params);
143 }
144 
setup_memory_tags(struct bd_info * bd)145 static void setup_memory_tags(struct bd_info *bd)
146 {
147 	int i;
148 
149 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
150 		params->hdr.tag = ATAG_MEM;
151 		params->hdr.size = tag_size (tag_mem32);
152 
153 		params->u.mem.start = bd->bi_dram[i].start;
154 		params->u.mem.size = bd->bi_dram[i].size;
155 
156 		params = tag_next (params);
157 	}
158 }
159 
setup_commandline_tag(struct bd_info * bd,char * commandline)160 static void setup_commandline_tag(struct bd_info *bd, char *commandline)
161 {
162 	char *p;
163 
164 	if (!commandline)
165 		return;
166 
167 	/* eat leading white space */
168 	for (p = commandline; *p == ' '; p++);
169 
170 	/* skip non-existent command lines so the kernel will still
171 	 * use its default command line.
172 	 */
173 	if (*p == '\0')
174 		return;
175 
176 	params->hdr.tag = ATAG_CMDLINE;
177 	params->hdr.size =
178 		(sizeof (struct tag_header) + strlen (p) + 1 + 4) >> 2;
179 
180 	strcpy (params->u.cmdline.cmdline, p);
181 
182 	params = tag_next (params);
183 }
184 
setup_initrd_tag(struct bd_info * bd,ulong initrd_start,ulong initrd_end)185 static void setup_initrd_tag(struct bd_info *bd, ulong initrd_start,
186 			     ulong initrd_end)
187 {
188 	/* an ATAG_INITRD node tells the kernel where the compressed
189 	 * ramdisk can be found. ATAG_RDIMG is a better name, actually.
190 	 */
191 	params->hdr.tag = ATAG_INITRD2;
192 	params->hdr.size = tag_size (tag_initrd);
193 
194 	params->u.initrd.start = initrd_start;
195 	params->u.initrd.size = initrd_end - initrd_start;
196 
197 	params = tag_next (params);
198 }
199 
setup_serial_tag(struct tag ** tmp)200 static void setup_serial_tag(struct tag **tmp)
201 {
202 	struct tag *params = *tmp;
203 	struct tag_serialnr serialnr;
204 
205 	get_board_serial(&serialnr);
206 	params->hdr.tag = ATAG_SERIAL;
207 	params->hdr.size = tag_size (tag_serialnr);
208 	params->u.serialnr.low = serialnr.low;
209 	params->u.serialnr.high= serialnr.high;
210 	params = tag_next (params);
211 	*tmp = params;
212 }
213 
setup_revision_tag(struct tag ** in_params)214 static void setup_revision_tag(struct tag **in_params)
215 {
216 	u32 rev = 0;
217 
218 	rev = get_board_rev();
219 	params->hdr.tag = ATAG_REVISION;
220 	params->hdr.size = tag_size (tag_revision);
221 	params->u.revision.rev = rev;
222 	params = tag_next (params);
223 }
224 
setup_end_tag(struct bd_info * bd)225 static void setup_end_tag(struct bd_info *bd)
226 {
227 	params->hdr.tag = ATAG_NONE;
228 	params->hdr.size = 0;
229 }
230 
setup_board_tags(struct tag ** in_params)231 __weak void setup_board_tags(struct tag **in_params) {}
232 
233 #ifdef CONFIG_ARM64
do_nonsec_virt_switch(void)234 static void do_nonsec_virt_switch(void)
235 {
236 	smp_kick_all_cpus();
237 	dcache_disable();	/* flush cache before swtiching to EL2 */
238 }
239 #endif
240 
board_prep_linux(bootm_headers_t * images)241 __weak void board_prep_linux(bootm_headers_t *images) { }
242 
243 /* Subcommand: PREP */
boot_prep_linux(bootm_headers_t * images)244 static void boot_prep_linux(bootm_headers_t *images)
245 {
246 	char *commandline = env_get("bootargs");
247 
248 	if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) {
249 #ifdef CONFIG_OF_LIBFDT
250 		debug("using: FDT\n");
251 		if (image_setup_linux(images)) {
252 			printf("FDT creation failed! hanging...");
253 			hang();
254 		}
255 #endif
256 	} else if (BOOTM_ENABLE_TAGS) {
257 		debug("using: ATAGS\n");
258 		setup_start_tag(gd->bd);
259 		if (BOOTM_ENABLE_SERIAL_TAG)
260 			setup_serial_tag(&params);
261 		if (BOOTM_ENABLE_CMDLINE_TAG)
262 			setup_commandline_tag(gd->bd, commandline);
263 		if (BOOTM_ENABLE_REVISION_TAG)
264 			setup_revision_tag(&params);
265 		if (BOOTM_ENABLE_MEMORY_TAGS)
266 			setup_memory_tags(gd->bd);
267 		if (BOOTM_ENABLE_INITRD_TAG) {
268 			/*
269 			 * In boot_ramdisk_high(), it may relocate ramdisk to
270 			 * a specified location. And set images->initrd_start &
271 			 * images->initrd_end to relocated ramdisk's start/end
272 			 * addresses. So use them instead of images->rd_start &
273 			 * images->rd_end when possible.
274 			 */
275 			if (images->initrd_start && images->initrd_end) {
276 				setup_initrd_tag(gd->bd, images->initrd_start,
277 						 images->initrd_end);
278 			} else if (images->rd_start && images->rd_end) {
279 				setup_initrd_tag(gd->bd, images->rd_start,
280 						 images->rd_end);
281 			}
282 		}
283 		setup_board_tags(&params);
284 		setup_end_tag(gd->bd);
285 	} else {
286 		printf("FDT and ATAGS support not compiled in - hanging\n");
287 		hang();
288 	}
289 
290 	board_prep_linux(images);
291 }
292 
armv7_boot_nonsec_default(void)293 __weak bool armv7_boot_nonsec_default(void)
294 {
295 #ifdef CONFIG_ARMV7_BOOT_SEC_DEFAULT
296 	return false;
297 #else
298 	return true;
299 #endif
300 }
301 
302 #ifdef CONFIG_ARMV7_NONSEC
armv7_boot_nonsec(void)303 bool armv7_boot_nonsec(void)
304 {
305 	char *s = env_get("bootm_boot_mode");
306 	bool nonsec = armv7_boot_nonsec_default();
307 
308 	if (s && !strcmp(s, "sec"))
309 		nonsec = false;
310 
311 	if (s && !strcmp(s, "nonsec"))
312 		nonsec = true;
313 
314 	return nonsec;
315 }
316 #endif
317 
318 #ifdef CONFIG_ARM64
update_os_arch_secondary_cores(uint8_t os_arch)319 __weak void update_os_arch_secondary_cores(uint8_t os_arch)
320 {
321 }
322 
323 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
switch_to_el1(void)324 static void switch_to_el1(void)
325 {
326 	if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
327 	    (images.os.arch == IH_ARCH_ARM))
328 		armv8_switch_to_el1(0, (u64)gd->bd->bi_arch_number,
329 				    (u64)images.ft_addr, 0,
330 				    (u64)images.ep,
331 				    ES_TO_AARCH32);
332 	else
333 		armv8_switch_to_el1((u64)images.ft_addr, 0, 0, 0,
334 				    images.ep,
335 				    ES_TO_AARCH64);
336 }
337 #endif
338 #endif
339 
340 /* Subcommand: GO */
boot_jump_linux(bootm_headers_t * images,int flag)341 static void boot_jump_linux(bootm_headers_t *images, int flag)
342 {
343 #ifdef CONFIG_ARM64
344 	void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
345 			void *res2);
346 	int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
347 
348 	kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
349 				void *res2))images->ep;
350 
351 	debug("## Transferring control to Linux (at address %lx)...\n",
352 		(ulong) kernel_entry);
353 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
354 
355 	announce_and_cleanup(fake);
356 
357 	if (!fake) {
358 #ifdef CONFIG_ARMV8_PSCI
359 		armv8_setup_psci();
360 #endif
361 		do_nonsec_virt_switch();
362 
363 		update_os_arch_secondary_cores(images->os.arch);
364 
365 #ifdef CONFIG_ARMV8_SWITCH_TO_EL1
366 		armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
367 				    (u64)switch_to_el1, ES_TO_AARCH64);
368 #else
369 		if ((IH_ARCH_DEFAULT == IH_ARCH_ARM64) &&
370 		    (images->os.arch == IH_ARCH_ARM))
371 			armv8_switch_to_el2(0, (u64)gd->bd->bi_arch_number,
372 					    (u64)images->ft_addr, 0,
373 					    (u64)images->ep,
374 					    ES_TO_AARCH32);
375 		else
376 			armv8_switch_to_el2((u64)images->ft_addr, 0, 0, 0,
377 					    images->ep,
378 					    ES_TO_AARCH64);
379 #endif
380 	}
381 #else
382 	unsigned long machid = gd->bd->bi_arch_number;
383 	char *s;
384 	void (*kernel_entry)(int zero, int arch, uint params);
385 	unsigned long r2;
386 	int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
387 
388 	kernel_entry = (void (*)(int, int, uint))images->ep;
389 #ifdef CONFIG_CPU_V7M
390 	ulong addr = (ulong)kernel_entry | 1;
391 	kernel_entry = (void *)addr;
392 #endif
393 	s = env_get("machid");
394 	if (s) {
395 		if (strict_strtoul(s, 16, &machid) < 0) {
396 			debug("strict_strtoul failed!\n");
397 			return;
398 		}
399 		printf("Using machid 0x%lx from environment\n", machid);
400 	}
401 
402 	debug("## Transferring control to Linux (at address %08lx)" \
403 		"...\n", (ulong) kernel_entry);
404 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
405 	announce_and_cleanup(fake);
406 
407 	if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
408 		r2 = (unsigned long)images->ft_addr;
409 	else
410 		r2 = gd->bd->bi_boot_params;
411 
412 	if (!fake) {
413 #ifdef CONFIG_ARMV7_NONSEC
414 		if (armv7_boot_nonsec()) {
415 			armv7_init_nonsec();
416 			secure_ram_addr(_do_nonsec_entry)(kernel_entry,
417 							  0, machid, r2);
418 		} else
419 #endif
420 			kernel_entry(0, machid, r2);
421 	}
422 #endif
423 }
424 
425 /* Main Entry point for arm bootm implementation
426  *
427  * Modeled after the powerpc implementation
428  * DIFFERENCE: Instead of calling prep and go at the end
429  * they are called if subcommand is equal 0.
430  */
do_bootm_linux(int flag,int argc,char * const argv[],bootm_headers_t * images)431 int do_bootm_linux(int flag, int argc, char *const argv[],
432 		   bootm_headers_t *images)
433 {
434 	/* No need for those on ARM */
435 	if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
436 		return -1;
437 
438 	if (flag & BOOTM_STATE_OS_PREP) {
439 		boot_prep_linux(images);
440 		return 0;
441 	}
442 
443 	if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
444 		boot_jump_linux(images, flag);
445 		return 0;
446 	}
447 
448 	boot_prep_linux(images);
449 	boot_jump_linux(images, flag);
450 	return 0;
451 }
452 
453 #if defined(CONFIG_BOOTM_VXWORKS)
boot_prep_vxworks(bootm_headers_t * images)454 void boot_prep_vxworks(bootm_headers_t *images)
455 {
456 #if defined(CONFIG_OF_LIBFDT)
457 	int off;
458 
459 	if (images->ft_addr) {
460 		off = fdt_path_offset(images->ft_addr, "/memory");
461 		if (off > 0) {
462 			if (arch_fixup_fdt(images->ft_addr))
463 				puts("## WARNING: fixup memory failed!\n");
464 		}
465 	}
466 #endif
467 	cleanup_before_linux();
468 }
boot_jump_vxworks(bootm_headers_t * images)469 void boot_jump_vxworks(bootm_headers_t *images)
470 {
471 #if defined(CONFIG_ARM64) && defined(CONFIG_ARMV8_PSCI)
472 	armv8_setup_psci();
473 	smp_kick_all_cpus();
474 #endif
475 
476 	/* ARM VxWorks requires device tree physical address to be passed */
477 	((void (*)(void *))images->ep)(images->ft_addr);
478 }
479 #endif
480