1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007 Michal Simek
4  * (C) Copyright 2004 Atmark Techno, Inc.
5  *
6  * Michal  SIMEK <monstr@monstr.eu>
7  * Yasushi SHOJI <yashi@atmark-techno.com>
8  */
9 
10 #include <bootm.h>
11 #include <bootstage.h>
12 #include <command.h>
13 #include <cpu_func.h>
14 #include <env.h>
15 #include <fdt_support.h>
16 #include <hang.h>
17 #include <image.h>
18 #include <log.h>
19 #include <asm/cache.h>
20 #include <asm/global_data.h>
21 #include <u-boot/zlib.h>
22 #include <asm/byteorder.h>
23 
24 DECLARE_GLOBAL_DATA_PTR;
25 
boot_jump_linux(struct bootm_headers * images,int flag)26 static void boot_jump_linux(struct bootm_headers *images, int flag)
27 {
28 	void (*thekernel)(char *cmdline, ulong rd, ulong dt);
29 	ulong dt = (ulong)images->ft_addr;
30 	ulong rd_start = images->initrd_start;
31 	ulong cmdline = images->cmdline_start;
32 	int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
33 
34 	thekernel = (void (*)(char *, ulong, ulong))images->ep;
35 
36 	debug("## Transferring control to Linux (at address 0x%08lx) ",
37 	      (ulong)thekernel);
38 	debug("cmdline 0x%08lx, ramdisk 0x%08lx, FDT 0x%08lx...\n",
39 	      cmdline, rd_start, dt);
40 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
41 
42 	printf("\nStarting kernel ...%s\n\n", fake ?
43 	       "(fake run for tracing)" : "");
44 	bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
45 
46 	flush_cache_all();
47 
48 	if (!fake) {
49 		/*
50 		 * Linux Kernel Parameters (passing device tree):
51 		 * r5: pointer to command line
52 		 * r6: pointer to ramdisk
53 		 * r7: pointer to the fdt, followed by the board info data
54 		 */
55 		thekernel((char *)cmdline, rd_start, dt);
56 		/* does not return */
57 	}
58 }
59 
boot_prep_linux(struct bootm_headers * images)60 static void boot_prep_linux(struct bootm_headers *images)
61 {
62 	if (CONFIG_IS_ENABLED(OF_LIBFDT) && IS_ENABLED(CONFIG_LMB) && images->ft_len) {
63 		debug("using: FDT\n");
64 		if (image_setup_linux(images)) {
65 			printf("FDT creation failed! hanging...");
66 			hang();
67 		}
68 	}
69 }
70 
do_bootm_linux(int flag,struct bootm_info * bmi)71 int do_bootm_linux(int flag, struct bootm_info *bmi)
72 {
73 	struct bootm_headers *images = bmi->images;
74 
75 	images->cmdline_start = (ulong)env_get("bootargs");
76 
77 	/* cmdline init is the part of 'prep' and nothing to do for 'bdt' */
78 	if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
79 		return -1;
80 
81 	if (flag & BOOTM_STATE_OS_PREP) {
82 		boot_prep_linux(images);
83 		return 0;
84 	}
85 
86 	if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
87 		boot_jump_linux(images, flag);
88 		return 0;
89 	}
90 
91 	boot_prep_linux(images);
92 	boot_jump_linux(images, flag);
93 	return 1;
94 }
95