xref: /rk3399_rockchip-uboot/arch/mips/lib/bootm.c (revision 25fc664f408e2e78623d03071884bafc62251553)
1 /*
2  * (C) Copyright 2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <image.h>
10 #include <asm/addrspace.h>
11 
12 DECLARE_GLOBAL_DATA_PTR;
13 
14 #define	LINUX_MAX_ENVS		256
15 #define	LINUX_MAX_ARGS		256
16 
17 #if defined(CONFIG_MALTA)
18 #define mips_boot_malta		1
19 #else
20 #define mips_boot_malta		0
21 #endif
22 
23 #if defined(CONFIG_MIPS_BOOT_CMDLINE_LEGACY)
24 #define mips_boot_cmdline_legacy	1
25 #else
26 #define mips_boot_cmdline_legacy	0
27 #endif
28 
29 static int linux_argc;
30 static char **linux_argv;
31 static char *linux_argp;
32 
33 static char **linux_env;
34 static char *linux_env_p;
35 static int linux_env_idx;
36 
37 static ulong arch_get_sp(void)
38 {
39 	ulong ret;
40 
41 	__asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
42 
43 	return ret;
44 }
45 
46 void arch_lmb_reserve(struct lmb *lmb)
47 {
48 	ulong sp;
49 
50 	sp = arch_get_sp();
51 	debug("## Current stack ends at 0x%08lx\n", sp);
52 
53 	/* adjust sp by 4K to be safe */
54 	sp -= 4096;
55 	lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
56 }
57 
58 static int boot_setup_linux(bootm_headers_t *images)
59 {
60 	int ret;
61 	ulong rd_len;
62 
63 	rd_len = images->rd_end - images->rd_start;
64 	ret = boot_ramdisk_high(&images->lmb, images->rd_start,
65 		rd_len, &images->initrd_start, &images->initrd_end);
66 	if (ret)
67 		return ret;
68 
69 	return 0;
70 }
71 
72 static void linux_cmdline_init(void)
73 {
74 	linux_argc = 1;
75 	linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
76 	linux_argv[0] = 0;
77 	linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
78 }
79 
80 static void linux_cmdline_set(const char *value, size_t len)
81 {
82 	linux_argv[linux_argc] = linux_argp;
83 	memcpy(linux_argp, value, len);
84 	linux_argp[len] = 0;
85 
86 	linux_argp += len + 1;
87 	linux_argc++;
88 }
89 
90 static void linux_cmdline_dump(void)
91 {
92 	int i;
93 
94 	debug("## cmdline argv at 0x%p, argp at 0x%p\n",
95 	      linux_argv, linux_argp);
96 
97 	for (i = 1; i < linux_argc; i++)
98 		debug("   arg %03d: %s\n", i, linux_argv[i]);
99 }
100 
101 static void linux_cmdline_legacy(bootm_headers_t *images)
102 {
103 	const char *bootargs, *next, *quote;
104 
105 	linux_cmdline_init();
106 
107 	bootargs = getenv("bootargs");
108 	if (!bootargs)
109 		return;
110 
111 	next = bootargs;
112 
113 	while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
114 		quote = strchr(bootargs, '"');
115 		next = strchr(bootargs, ' ');
116 
117 		while (next && quote && quote < next) {
118 			/*
119 			 * we found a left quote before the next blank
120 			 * now we have to find the matching right quote
121 			 */
122 			next = strchr(quote + 1, '"');
123 			if (next) {
124 				quote = strchr(next + 1, '"');
125 				next = strchr(next + 1, ' ');
126 			}
127 		}
128 
129 		if (!next)
130 			next = bootargs + strlen(bootargs);
131 
132 		linux_cmdline_set(bootargs, next - bootargs);
133 
134 		if (*next)
135 			next++;
136 
137 		bootargs = next;
138 	}
139 }
140 
141 static void boot_cmdline_linux(bootm_headers_t *images)
142 {
143 	if (mips_boot_cmdline_legacy) {
144 		linux_cmdline_legacy(images);
145 		linux_cmdline_dump();
146 	}
147 }
148 
149 static void linux_env_init(void)
150 {
151 	linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
152 	linux_env[0] = 0;
153 	linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
154 	linux_env_idx = 0;
155 }
156 
157 static void linux_env_set(const char *env_name, const char *env_val)
158 {
159 	if (linux_env_idx < LINUX_MAX_ENVS - 1) {
160 		linux_env[linux_env_idx] = linux_env_p;
161 
162 		strcpy(linux_env_p, env_name);
163 		linux_env_p += strlen(env_name);
164 
165 		if (mips_boot_malta) {
166 			linux_env_p++;
167 			linux_env[++linux_env_idx] = linux_env_p;
168 		} else {
169 			*linux_env_p++ = '=';
170 		}
171 
172 		strcpy(linux_env_p, env_val);
173 		linux_env_p += strlen(env_val);
174 
175 		linux_env_p++;
176 		linux_env[++linux_env_idx] = 0;
177 	}
178 }
179 
180 static void boot_prep_linux(bootm_headers_t *images)
181 {
182 	char env_buf[12];
183 	const char *cp;
184 	ulong rd_start, rd_size;
185 
186 #ifdef CONFIG_MEMSIZE_IN_BYTES
187 	sprintf(env_buf, "%lu", (ulong)gd->ram_size);
188 	debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
189 #else
190 	sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
191 	debug("## Giving linux memsize in MB, %lu\n",
192 	      (ulong)(gd->ram_size >> 20));
193 #endif /* CONFIG_MEMSIZE_IN_BYTES */
194 
195 	rd_start = UNCACHED_SDRAM(images->initrd_start);
196 	rd_size = images->initrd_end - images->initrd_start;
197 
198 	linux_env_init();
199 
200 	linux_env_set("memsize", env_buf);
201 
202 	sprintf(env_buf, "0x%08lX", rd_start);
203 	linux_env_set("initrd_start", env_buf);
204 
205 	sprintf(env_buf, "0x%lX", rd_size);
206 	linux_env_set("initrd_size", env_buf);
207 
208 	sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
209 	linux_env_set("flash_start", env_buf);
210 
211 	sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
212 	linux_env_set("flash_size", env_buf);
213 
214 	cp = getenv("ethaddr");
215 	if (cp)
216 		linux_env_set("ethaddr", cp);
217 
218 	cp = getenv("eth1addr");
219 	if (cp)
220 		linux_env_set("eth1addr", cp);
221 
222 	if (mips_boot_malta) {
223 		sprintf(env_buf, "%un8r", gd->baudrate);
224 		linux_env_set("modetty0", env_buf);
225 	}
226 }
227 
228 static void boot_jump_linux(bootm_headers_t *images)
229 {
230 	typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
231 	kernel_entry_t kernel = (kernel_entry_t) images->ep;
232 	ulong linux_extra = 0;
233 
234 	debug("## Transferring control to Linux (at address %p) ...\n", kernel);
235 
236 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
237 
238 	if (mips_boot_malta)
239 		linux_extra = gd->ram_size;
240 
241 	/* we assume that the kernel is in place */
242 	printf("\nStarting kernel ...\n\n");
243 
244 	kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
245 }
246 
247 int do_bootm_linux(int flag, int argc, char * const argv[],
248 			bootm_headers_t *images)
249 {
250 	int ret;
251 
252 	/* No need for those on MIPS */
253 	if (flag & BOOTM_STATE_OS_BD_T)
254 		return -1;
255 
256 	if (flag & BOOTM_STATE_OS_CMDLINE) {
257 		boot_cmdline_linux(images);
258 		return 0;
259 	}
260 
261 	if (flag & BOOTM_STATE_OS_PREP) {
262 		boot_prep_linux(images);
263 		return 0;
264 	}
265 
266 	if (flag & BOOTM_STATE_OS_GO) {
267 		boot_jump_linux(images);
268 		return 0;
269 	}
270 
271 	ret = boot_setup_linux(images);
272 	if (ret)
273 		return ret;
274 
275 	boot_cmdline_linux(images);
276 	boot_prep_linux(images);
277 	boot_jump_linux(images);
278 
279 	/* does not return */
280 	return 1;
281 }
282