3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/addrspace.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 #define LINUX_MAX_ENVS 256
15 #define LINUX_MAX_ARGS 256
17 #if defined(CONFIG_MALTA)
18 #define mips_boot_malta 1
20 #define mips_boot_malta 0
23 #if defined(CONFIG_MIPS_BOOT_CMDLINE_LEGACY)
24 #define mips_boot_cmdline_legacy 1
26 #define mips_boot_cmdline_legacy 0
29 #if defined(CONFIG_MIPS_BOOT_ENV_LEGACY)
30 #define mips_boot_env_legacy 1
32 #define mips_boot_env_legacy 0
35 static int linux_argc;
36 static char **linux_argv;
37 static char *linux_argp;
39 static char **linux_env;
40 static char *linux_env_p;
41 static int linux_env_idx;
43 static ulong arch_get_sp(void)
47 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
52 void arch_lmb_reserve(struct lmb *lmb)
57 debug("## Current stack ends at 0x%08lx\n", sp);
59 /* adjust sp by 4K to be safe */
61 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
64 static int boot_setup_linux(bootm_headers_t *images)
69 rd_len = images->rd_end - images->rd_start;
70 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
71 rd_len, &images->initrd_start, &images->initrd_end);
78 static void linux_cmdline_init(void)
81 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
83 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
86 static void linux_cmdline_set(const char *value, size_t len)
88 linux_argv[linux_argc] = linux_argp;
89 memcpy(linux_argp, value, len);
92 linux_argp += len + 1;
96 static void linux_cmdline_dump(void)
100 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
101 linux_argv, linux_argp);
103 for (i = 1; i < linux_argc; i++)
104 debug(" arg %03d: %s\n", i, linux_argv[i]);
107 static void linux_cmdline_legacy(bootm_headers_t *images)
109 const char *bootargs, *next, *quote;
111 linux_cmdline_init();
113 bootargs = getenv("bootargs");
119 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
120 quote = strchr(bootargs, '"');
121 next = strchr(bootargs, ' ');
123 while (next && quote && quote < next) {
125 * we found a left quote before the next blank
126 * now we have to find the matching right quote
128 next = strchr(quote + 1, '"');
130 quote = strchr(next + 1, '"');
131 next = strchr(next + 1, ' ');
136 next = bootargs + strlen(bootargs);
138 linux_cmdline_set(bootargs, next - bootargs);
147 static void linux_cmdline_append(bootm_headers_t *images)
150 ulong mem, rd_start, rd_size;
153 mem = gd->ram_size >> 20;
154 sprintf(buf, "mem=%luM", mem);
155 linux_cmdline_set(buf, strlen(buf));
157 /* append rd_start and rd_size */
158 rd_start = images->initrd_start;
159 rd_size = images->initrd_end - images->initrd_start;
162 sprintf(buf, "rd_start=0x%08lX", rd_start);
163 linux_cmdline_set(buf, strlen(buf));
164 sprintf(buf, "rd_size=0x%lX", rd_size);
165 linux_cmdline_set(buf, strlen(buf));
169 static void boot_cmdline_linux(bootm_headers_t *images)
171 if (mips_boot_cmdline_legacy) {
172 linux_cmdline_legacy(images);
174 if (!mips_boot_env_legacy)
175 linux_cmdline_append(images);
177 linux_cmdline_dump();
181 static void linux_env_init(void)
183 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
185 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
189 static void linux_env_set(const char *env_name, const char *env_val)
191 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
192 linux_env[linux_env_idx] = linux_env_p;
194 strcpy(linux_env_p, env_name);
195 linux_env_p += strlen(env_name);
197 if (mips_boot_malta) {
199 linux_env[++linux_env_idx] = linux_env_p;
201 *linux_env_p++ = '=';
204 strcpy(linux_env_p, env_val);
205 linux_env_p += strlen(env_val);
208 linux_env[++linux_env_idx] = 0;
212 static void linux_env_legacy(bootm_headers_t *images)
216 ulong rd_start, rd_size;
218 #ifdef CONFIG_MEMSIZE_IN_BYTES
219 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
220 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
222 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
223 debug("## Giving linux memsize in MB, %lu\n",
224 (ulong)(gd->ram_size >> 20));
225 #endif /* CONFIG_MEMSIZE_IN_BYTES */
227 rd_start = UNCACHED_SDRAM(images->initrd_start);
228 rd_size = images->initrd_end - images->initrd_start;
232 linux_env_set("memsize", env_buf);
234 sprintf(env_buf, "0x%08lX", rd_start);
235 linux_env_set("initrd_start", env_buf);
237 sprintf(env_buf, "0x%lX", rd_size);
238 linux_env_set("initrd_size", env_buf);
240 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
241 linux_env_set("flash_start", env_buf);
243 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
244 linux_env_set("flash_size", env_buf);
246 cp = getenv("ethaddr");
248 linux_env_set("ethaddr", cp);
250 cp = getenv("eth1addr");
252 linux_env_set("eth1addr", cp);
254 if (mips_boot_malta) {
255 sprintf(env_buf, "%un8r", gd->baudrate);
256 linux_env_set("modetty0", env_buf);
260 static void boot_prep_linux(bootm_headers_t *images)
262 if (mips_boot_env_legacy)
263 linux_env_legacy(images);
266 static void boot_jump_linux(bootm_headers_t *images)
268 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
269 kernel_entry_t kernel = (kernel_entry_t) images->ep;
270 ulong linux_extra = 0;
272 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
274 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
277 linux_extra = gd->ram_size;
279 /* we assume that the kernel is in place */
280 printf("\nStarting kernel ...\n\n");
282 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
285 int do_bootm_linux(int flag, int argc, char * const argv[],
286 bootm_headers_t *images)
290 /* No need for those on MIPS */
291 if (flag & BOOTM_STATE_OS_BD_T)
294 if (flag & BOOTM_STATE_OS_CMDLINE) {
295 boot_cmdline_linux(images);
299 if (flag & BOOTM_STATE_OS_PREP) {
300 boot_prep_linux(images);
304 if (flag & BOOTM_STATE_OS_GO) {
305 boot_jump_linux(images);
309 ret = boot_setup_linux(images);
313 boot_cmdline_linux(images);
314 boot_prep_linux(images);
315 boot_jump_linux(images);
317 /* does not return */