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 static int linux_argc;
21 static char **linux_argv;
22 static char *linux_argp;
24 static char **linux_env;
25 static char *linux_env_p;
26 static int linux_env_idx;
28 static ulong arch_get_sp(void)
32 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
37 void arch_lmb_reserve(struct lmb *lmb)
42 debug("## Current stack ends at 0x%08lx\n", sp);
44 /* adjust sp by 4K to be safe */
46 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
49 static void linux_cmdline_init(void)
52 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
54 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
57 static void linux_cmdline_set(const char *value, size_t len)
59 linux_argv[linux_argc] = linux_argp;
60 memcpy(linux_argp, value, len);
63 linux_argp += len + 1;
67 static void linux_cmdline_dump(void)
71 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
72 linux_argv, linux_argp);
74 for (i = 1; i < linux_argc; i++)
75 debug(" arg %03d: %s\n", i, linux_argv[i]);
78 static void boot_cmdline_linux(bootm_headers_t *images)
80 const char *bootargs, *next, *quote;
84 bootargs = getenv("bootargs");
90 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
91 quote = strchr(bootargs, '"');
92 next = strchr(bootargs, ' ');
94 while (next && quote && quote < next) {
96 * we found a left quote before the next blank
97 * now we have to find the matching right quote
99 next = strchr(quote + 1, '"');
101 quote = strchr(next + 1, '"');
102 next = strchr(next + 1, ' ');
107 next = bootargs + strlen(bootargs);
109 linux_cmdline_set(bootargs, next - bootargs);
117 linux_cmdline_dump();
120 static void linux_env_init(void)
122 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
124 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
128 static void linux_env_set(const char *env_name, const char *env_val)
130 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
131 linux_env[linux_env_idx] = linux_env_p;
133 strcpy(linux_env_p, env_name);
134 linux_env_p += strlen(env_name);
136 *linux_env_p++ = '=';
138 strcpy(linux_env_p, env_val);
139 linux_env_p += strlen(env_val);
142 linux_env[++linux_env_idx] = 0;
146 static void boot_prep_linux(bootm_headers_t *images)
150 ulong rd_start, rd_size;
152 #ifdef CONFIG_MEMSIZE_IN_BYTES
153 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
154 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
156 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
157 debug("## Giving linux memsize in MB, %lu\n",
158 (ulong)(gd->ram_size >> 20));
159 #endif /* CONFIG_MEMSIZE_IN_BYTES */
161 rd_start = UNCACHED_SDRAM(images->initrd_start);
162 rd_size = images->initrd_end - images->initrd_start;
166 linux_env_set("memsize", env_buf);
168 sprintf(env_buf, "0x%08lX", rd_start);
169 linux_env_set("initrd_start", env_buf);
171 sprintf(env_buf, "0x%lX", rd_size);
172 linux_env_set("initrd_size", env_buf);
174 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
175 linux_env_set("flash_start", env_buf);
177 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
178 linux_env_set("flash_size", env_buf);
180 cp = getenv("ethaddr");
182 linux_env_set("ethaddr", cp);
184 cp = getenv("eth1addr");
186 linux_env_set("eth1addr", cp);
189 static void boot_jump_linux(bootm_headers_t *images)
191 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
192 kernel_entry_t kernel = (kernel_entry_t) images->ep;
194 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
196 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
198 /* we assume that the kernel is in place */
199 printf("\nStarting kernel ...\n\n");
201 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0);
204 int do_bootm_linux(int flag, int argc, char * const argv[],
205 bootm_headers_t *images)
207 /* No need for those on MIPS */
208 if (flag & BOOTM_STATE_OS_BD_T)
211 if (flag & BOOTM_STATE_OS_CMDLINE) {
212 boot_cmdline_linux(images);
216 if (flag & BOOTM_STATE_OS_PREP) {
217 boot_prep_linux(images);
221 if (flag & BOOTM_STATE_OS_GO) {
222 boot_jump_linux(images);
226 boot_cmdline_linux(images);
227 boot_prep_linux(images);
228 boot_jump_linux(images);
230 /* does not return */