3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <u-boot/zlib.h>
12 #include <asm/byteorder.h>
13 #include <asm/addrspace.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 #define LINUX_MAX_ENVS 256
18 #define LINUX_MAX_ARGS 256
20 #if defined(CONFIG_QEMU_MALTA)
21 #define mips_boot_qemu_malta 1
23 #define mips_boot_qemu_malta 0
26 static int linux_argc;
27 static char **linux_argv;
28 static char *linux_argp;
30 static char **linux_env;
31 static char *linux_env_p;
32 static int linux_env_idx;
34 static ulong arch_get_sp(void)
38 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
43 void arch_lmb_reserve(struct lmb *lmb)
48 debug("## Current stack ends at 0x%08lx\n", sp);
50 /* adjust sp by 4K to be safe */
52 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
55 static void linux_cmdline_init(void)
58 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
60 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
63 static void linux_cmdline_set(const char *value, size_t len)
65 linux_argv[linux_argc] = linux_argp;
66 memcpy(linux_argp, value, len);
69 linux_argp += len + 1;
73 static void linux_cmdline_dump(void)
77 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
78 linux_argv, linux_argp);
80 for (i = 1; i < linux_argc; i++)
81 debug(" arg %03d: %s\n", i, linux_argv[i]);
84 static void boot_cmdline_linux(bootm_headers_t *images)
86 const char *bootargs, *next, *quote;
90 bootargs = getenv("bootargs");
96 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
97 quote = strchr(bootargs, '"');
98 next = strchr(bootargs, ' ');
100 while (next && quote && quote < next) {
102 * we found a left quote before the next blank
103 * now we have to find the matching right quote
105 next = strchr(quote + 1, '"');
107 quote = strchr(next + 1, '"');
108 next = strchr(next + 1, ' ');
113 next = bootargs + strlen(bootargs);
115 linux_cmdline_set(bootargs, next - bootargs);
123 linux_cmdline_dump();
126 static void linux_env_init(void)
128 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
130 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
134 static void linux_env_set(const char *env_name, const char *env_val)
136 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
137 linux_env[linux_env_idx] = linux_env_p;
139 strcpy(linux_env_p, env_name);
140 linux_env_p += strlen(env_name);
142 if (mips_boot_qemu_malta) {
144 linux_env[++linux_env_idx] = linux_env_p;
146 *linux_env_p++ = '=';
149 strcpy(linux_env_p, env_val);
150 linux_env_p += strlen(env_val);
153 linux_env[++linux_env_idx] = 0;
157 static void boot_prep_linux(bootm_headers_t *images)
161 ulong rd_start, rd_size;
163 #ifdef CONFIG_MEMSIZE_IN_BYTES
164 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
165 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
167 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
168 debug("## Giving linux memsize in MB, %lu\n",
169 (ulong)(gd->ram_size >> 20));
170 #endif /* CONFIG_MEMSIZE_IN_BYTES */
172 rd_start = UNCACHED_SDRAM(images->initrd_start);
173 rd_size = images->initrd_end - images->initrd_start;
177 linux_env_set("memsize", env_buf);
179 sprintf(env_buf, "0x%08lX", rd_start);
180 linux_env_set("initrd_start", env_buf);
182 sprintf(env_buf, "0x%lX", rd_size);
183 linux_env_set("initrd_size", env_buf);
185 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
186 linux_env_set("flash_start", env_buf);
188 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
189 linux_env_set("flash_size", env_buf);
191 cp = getenv("ethaddr");
193 linux_env_set("ethaddr", cp);
195 cp = getenv("eth1addr");
197 linux_env_set("eth1addr", cp);
199 if (mips_boot_qemu_malta)
200 linux_env_set("modetty0", "38400n8r");
203 static void boot_jump_linux(bootm_headers_t *images)
205 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
206 kernel_entry_t kernel = (kernel_entry_t) images->ep;
207 ulong linux_extra = 0;
209 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
211 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
213 if (mips_boot_qemu_malta)
214 linux_extra = gd->ram_size;
216 /* we assume that the kernel is in place */
217 printf("\nStarting kernel ...\n\n");
219 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
222 int do_bootm_linux(int flag, int argc, char * const argv[],
223 bootm_headers_t *images)
225 /* No need for those on MIPS */
226 if (flag & BOOTM_STATE_OS_BD_T)
229 if (flag & BOOTM_STATE_OS_CMDLINE) {
230 boot_cmdline_linux(images);
234 if (flag & BOOTM_STATE_OS_PREP) {
235 boot_prep_linux(images);
239 if (flag & BOOTM_STATE_OS_GO) {
240 boot_jump_linux(images);
244 boot_cmdline_linux(images);
245 boot_prep_linux(images);
246 boot_jump_linux(images);
248 /* does not return */