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_MALTA)
21 #define mips_boot_malta 1
23 #define mips_boot_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 int boot_setup_linux(bootm_headers_t *images)
60 rd_len = images->rd_end - images->rd_start;
61 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
62 rd_len, &images->initrd_start, &images->initrd_end);
69 static void linux_cmdline_init(void)
72 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
74 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
77 static void linux_cmdline_set(const char *value, size_t len)
79 linux_argv[linux_argc] = linux_argp;
80 memcpy(linux_argp, value, len);
83 linux_argp += len + 1;
87 static void linux_cmdline_dump(void)
91 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
92 linux_argv, linux_argp);
94 for (i = 1; i < linux_argc; i++)
95 debug(" arg %03d: %s\n", i, linux_argv[i]);
98 static void boot_cmdline_linux(bootm_headers_t *images)
100 const char *bootargs, *next, *quote;
102 linux_cmdline_init();
104 bootargs = getenv("bootargs");
110 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
111 quote = strchr(bootargs, '"');
112 next = strchr(bootargs, ' ');
114 while (next && quote && quote < next) {
116 * we found a left quote before the next blank
117 * now we have to find the matching right quote
119 next = strchr(quote + 1, '"');
121 quote = strchr(next + 1, '"');
122 next = strchr(next + 1, ' ');
127 next = bootargs + strlen(bootargs);
129 linux_cmdline_set(bootargs, next - bootargs);
137 linux_cmdline_dump();
140 static void linux_env_init(void)
142 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
144 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
148 static void linux_env_set(const char *env_name, const char *env_val)
150 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
151 linux_env[linux_env_idx] = linux_env_p;
153 strcpy(linux_env_p, env_name);
154 linux_env_p += strlen(env_name);
156 if (mips_boot_malta) {
158 linux_env[++linux_env_idx] = linux_env_p;
160 *linux_env_p++ = '=';
163 strcpy(linux_env_p, env_val);
164 linux_env_p += strlen(env_val);
167 linux_env[++linux_env_idx] = 0;
171 static void boot_prep_linux(bootm_headers_t *images)
175 ulong rd_start, rd_size;
177 #ifdef CONFIG_MEMSIZE_IN_BYTES
178 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
179 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
181 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
182 debug("## Giving linux memsize in MB, %lu\n",
183 (ulong)(gd->ram_size >> 20));
184 #endif /* CONFIG_MEMSIZE_IN_BYTES */
186 rd_start = UNCACHED_SDRAM(images->initrd_start);
187 rd_size = images->initrd_end - images->initrd_start;
191 linux_env_set("memsize", env_buf);
193 sprintf(env_buf, "0x%08lX", rd_start);
194 linux_env_set("initrd_start", env_buf);
196 sprintf(env_buf, "0x%lX", rd_size);
197 linux_env_set("initrd_size", env_buf);
199 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
200 linux_env_set("flash_start", env_buf);
202 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
203 linux_env_set("flash_size", env_buf);
205 cp = getenv("ethaddr");
207 linux_env_set("ethaddr", cp);
209 cp = getenv("eth1addr");
211 linux_env_set("eth1addr", cp);
213 if (mips_boot_malta) {
214 sprintf(env_buf, "%un8r", gd->baudrate);
215 linux_env_set("modetty0", env_buf);
219 static void boot_jump_linux(bootm_headers_t *images)
221 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
222 kernel_entry_t kernel = (kernel_entry_t) images->ep;
223 ulong linux_extra = 0;
225 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
227 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
230 linux_extra = gd->ram_size;
232 /* we assume that the kernel is in place */
233 printf("\nStarting kernel ...\n\n");
235 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
238 int do_bootm_linux(int flag, int argc, char * const argv[],
239 bootm_headers_t *images)
243 /* No need for those on MIPS */
244 if (flag & BOOTM_STATE_OS_BD_T)
247 if (flag & BOOTM_STATE_OS_CMDLINE) {
248 boot_cmdline_linux(images);
252 if (flag & BOOTM_STATE_OS_PREP) {
253 boot_prep_linux(images);
257 if (flag & BOOTM_STATE_OS_GO) {
258 boot_jump_linux(images);
262 ret = boot_setup_linux(images);
266 boot_cmdline_linux(images);
267 boot_prep_linux(images);
268 boot_jump_linux(images);
270 /* does not return */