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 static int linux_argc;
30 static char **linux_argv;
31 static char *linux_argp;
33 static char **linux_env;
34 static char *linux_env_p;
35 static int linux_env_idx;
37 static ulong arch_get_sp(void)
41 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
46 void arch_lmb_reserve(struct lmb *lmb)
51 debug("## Current stack ends at 0x%08lx\n", sp);
53 /* adjust sp by 4K to be safe */
55 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
58 static int boot_setup_linux(bootm_headers_t *images)
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);
72 static void linux_cmdline_init(void)
75 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
77 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
80 static void linux_cmdline_set(const char *value, size_t len)
82 linux_argv[linux_argc] = linux_argp;
83 memcpy(linux_argp, value, len);
86 linux_argp += len + 1;
90 static void linux_cmdline_dump(void)
94 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
95 linux_argv, linux_argp);
97 for (i = 1; i < linux_argc; i++)
98 debug(" arg %03d: %s\n", i, linux_argv[i]);
101 static void linux_cmdline_legacy(bootm_headers_t *images)
103 const char *bootargs, *next, *quote;
105 linux_cmdline_init();
107 bootargs = getenv("bootargs");
113 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
114 quote = strchr(bootargs, '"');
115 next = strchr(bootargs, ' ');
117 while (next && quote && quote < next) {
119 * we found a left quote before the next blank
120 * now we have to find the matching right quote
122 next = strchr(quote + 1, '"');
124 quote = strchr(next + 1, '"');
125 next = strchr(next + 1, ' ');
130 next = bootargs + strlen(bootargs);
132 linux_cmdline_set(bootargs, next - bootargs);
141 static void boot_cmdline_linux(bootm_headers_t *images)
143 if (mips_boot_cmdline_legacy) {
144 linux_cmdline_legacy(images);
145 linux_cmdline_dump();
149 static void linux_env_init(void)
151 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
153 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
157 static void linux_env_set(const char *env_name, const char *env_val)
159 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
160 linux_env[linux_env_idx] = linux_env_p;
162 strcpy(linux_env_p, env_name);
163 linux_env_p += strlen(env_name);
165 if (mips_boot_malta) {
167 linux_env[++linux_env_idx] = linux_env_p;
169 *linux_env_p++ = '=';
172 strcpy(linux_env_p, env_val);
173 linux_env_p += strlen(env_val);
176 linux_env[++linux_env_idx] = 0;
180 static void boot_prep_linux(bootm_headers_t *images)
184 ulong rd_start, rd_size;
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);
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 */
195 rd_start = UNCACHED_SDRAM(images->initrd_start);
196 rd_size = images->initrd_end - images->initrd_start;
200 linux_env_set("memsize", env_buf);
202 sprintf(env_buf, "0x%08lX", rd_start);
203 linux_env_set("initrd_start", env_buf);
205 sprintf(env_buf, "0x%lX", rd_size);
206 linux_env_set("initrd_size", env_buf);
208 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
209 linux_env_set("flash_start", env_buf);
211 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
212 linux_env_set("flash_size", env_buf);
214 cp = getenv("ethaddr");
216 linux_env_set("ethaddr", cp);
218 cp = getenv("eth1addr");
220 linux_env_set("eth1addr", cp);
222 if (mips_boot_malta) {
223 sprintf(env_buf, "%un8r", gd->baudrate);
224 linux_env_set("modetty0", env_buf);
228 static void boot_jump_linux(bootm_headers_t *images)
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;
234 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
236 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
239 linux_extra = gd->ram_size;
241 /* we assume that the kernel is in place */
242 printf("\nStarting kernel ...\n\n");
244 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
247 int do_bootm_linux(int flag, int argc, char * const argv[],
248 bootm_headers_t *images)
252 /* No need for those on MIPS */
253 if (flag & BOOTM_STATE_OS_BD_T)
256 if (flag & BOOTM_STATE_OS_CMDLINE) {
257 boot_cmdline_linux(images);
261 if (flag & BOOTM_STATE_OS_PREP) {
262 boot_prep_linux(images);
266 if (flag & BOOTM_STATE_OS_GO) {
267 boot_jump_linux(images);
271 ret = boot_setup_linux(images);
275 boot_cmdline_linux(images);
276 boot_prep_linux(images);
277 boot_jump_linux(images);
279 /* does not return */