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 static int linux_argc;
24 static char **linux_argv;
25 static char *linux_argp;
27 static char **linux_env;
28 static char *linux_env_p;
29 static int linux_env_idx;
31 static ulong arch_get_sp(void)
35 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
40 void arch_lmb_reserve(struct lmb *lmb)
45 debug("## Current stack ends at 0x%08lx\n", sp);
47 /* adjust sp by 4K to be safe */
49 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
52 static int boot_setup_linux(bootm_headers_t *images)
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);
66 static void linux_cmdline_init(void)
69 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
71 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
74 static void linux_cmdline_set(const char *value, size_t len)
76 linux_argv[linux_argc] = linux_argp;
77 memcpy(linux_argp, value, len);
80 linux_argp += len + 1;
84 static void linux_cmdline_dump(void)
88 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
89 linux_argv, linux_argp);
91 for (i = 1; i < linux_argc; i++)
92 debug(" arg %03d: %s\n", i, linux_argv[i]);
95 static void boot_cmdline_linux(bootm_headers_t *images)
97 const char *bootargs, *next, *quote;
101 bootargs = getenv("bootargs");
107 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
108 quote = strchr(bootargs, '"');
109 next = strchr(bootargs, ' ');
111 while (next && quote && quote < next) {
113 * we found a left quote before the next blank
114 * now we have to find the matching right quote
116 next = strchr(quote + 1, '"');
118 quote = strchr(next + 1, '"');
119 next = strchr(next + 1, ' ');
124 next = bootargs + strlen(bootargs);
126 linux_cmdline_set(bootargs, next - bootargs);
134 linux_cmdline_dump();
137 static void linux_env_init(void)
139 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
141 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
145 static void linux_env_set(const char *env_name, const char *env_val)
147 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
148 linux_env[linux_env_idx] = linux_env_p;
150 strcpy(linux_env_p, env_name);
151 linux_env_p += strlen(env_name);
153 if (mips_boot_malta) {
155 linux_env[++linux_env_idx] = linux_env_p;
157 *linux_env_p++ = '=';
160 strcpy(linux_env_p, env_val);
161 linux_env_p += strlen(env_val);
164 linux_env[++linux_env_idx] = 0;
168 static void boot_prep_linux(bootm_headers_t *images)
172 ulong rd_start, rd_size;
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);
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 */
183 rd_start = UNCACHED_SDRAM(images->initrd_start);
184 rd_size = images->initrd_end - images->initrd_start;
188 linux_env_set("memsize", env_buf);
190 sprintf(env_buf, "0x%08lX", rd_start);
191 linux_env_set("initrd_start", env_buf);
193 sprintf(env_buf, "0x%lX", rd_size);
194 linux_env_set("initrd_size", env_buf);
196 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
197 linux_env_set("flash_start", env_buf);
199 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
200 linux_env_set("flash_size", env_buf);
202 cp = getenv("ethaddr");
204 linux_env_set("ethaddr", cp);
206 cp = getenv("eth1addr");
208 linux_env_set("eth1addr", cp);
210 if (mips_boot_malta) {
211 sprintf(env_buf, "%un8r", gd->baudrate);
212 linux_env_set("modetty0", env_buf);
216 static void boot_jump_linux(bootm_headers_t *images)
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;
222 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
224 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
227 linux_extra = gd->ram_size;
229 /* we assume that the kernel is in place */
230 printf("\nStarting kernel ...\n\n");
232 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
235 int do_bootm_linux(int flag, int argc, char * const argv[],
236 bootm_headers_t *images)
240 /* No need for those on MIPS */
241 if (flag & BOOTM_STATE_OS_BD_T)
244 if (flag & BOOTM_STATE_OS_CMDLINE) {
245 boot_cmdline_linux(images);
249 if (flag & BOOTM_STATE_OS_PREP) {
250 boot_prep_linux(images);
254 if (flag & BOOTM_STATE_OS_GO) {
255 boot_jump_linux(images);
259 ret = boot_setup_linux(images);
263 boot_cmdline_linux(images);
264 boot_prep_linux(images);
265 boot_jump_linux(images);
267 /* does not return */