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