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 void linux_params_init(void);
29 static void linux_env_set(char *env_name, char *env_val);
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 void linux_cmdline_init(void)
55 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
57 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
60 static void linux_cmdline_set(const char *value, size_t len)
62 linux_argv[linux_argc] = linux_argp;
63 memcpy(linux_argp, value, len);
66 linux_argp += len + 1;
70 static void linux_cmdline_dump(void)
74 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
75 linux_argv, linux_argp);
77 for (i = 1; i < linux_argc; i++)
78 debug(" arg %03d: %s\n", i, linux_argv[i]);
81 static void boot_cmdline_linux(bootm_headers_t *images)
83 const char *bootargs, *next, *quote;
87 bootargs = getenv("bootargs");
93 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
94 quote = strchr(bootargs, '"');
95 next = strchr(bootargs, ' ');
97 while (next && quote && quote < next) {
99 * we found a left quote before the next blank
100 * now we have to find the matching right quote
102 next = strchr(quote + 1, '"');
104 quote = strchr(next + 1, '"');
105 next = strchr(next + 1, ' ');
110 next = bootargs + strlen(bootargs);
112 linux_cmdline_set(bootargs, next - bootargs);
120 linux_cmdline_dump();
123 static void boot_prep_linux(bootm_headers_t *images)
130 #ifdef CONFIG_MEMSIZE_IN_BYTES
131 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
132 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
134 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
135 debug("## Giving linux memsize in MB, %lu\n",
136 (ulong)(gd->ram_size >> 20));
137 #endif /* CONFIG_MEMSIZE_IN_BYTES */
139 linux_env_set("memsize", env_buf);
141 sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start));
142 linux_env_set("initrd_start", env_buf);
144 sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start));
145 linux_env_set("initrd_size", env_buf);
147 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
148 linux_env_set("flash_start", env_buf);
150 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
151 linux_env_set("flash_size", env_buf);
153 cp = getenv("ethaddr");
155 linux_env_set("ethaddr", cp);
157 cp = getenv("eth1addr");
159 linux_env_set("eth1addr", cp);
162 static void boot_jump_linux(bootm_headers_t *images)
164 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
165 kernel_entry_t kernel = (kernel_entry_t) images->ep;
167 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
169 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
171 /* we assume that the kernel is in place */
172 printf("\nStarting kernel ...\n\n");
174 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0);
177 int do_bootm_linux(int flag, int argc, char * const argv[],
178 bootm_headers_t *images)
180 /* No need for those on MIPS */
181 if (flag & BOOTM_STATE_OS_BD_T)
184 if (flag & BOOTM_STATE_OS_CMDLINE) {
185 boot_cmdline_linux(images);
189 if (flag & BOOTM_STATE_OS_PREP) {
190 boot_prep_linux(images);
194 if (flag & BOOTM_STATE_OS_GO) {
195 boot_jump_linux(images);
199 boot_cmdline_linux(images);
200 boot_prep_linux(images);
201 boot_jump_linux(images);
203 /* does not return */
207 static void linux_params_init(void)
209 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
211 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
215 static void linux_env_set(char *env_name, char *env_val)
217 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
218 linux_env[linux_env_idx] = linux_env_p;
220 strcpy(linux_env_p, env_name);
221 linux_env_p += strlen(env_name);
223 strcpy(linux_env_p, "=");
226 strcpy(linux_env_p, env_val);
227 linux_env_p += strlen(env_val);
230 linux_env[++linux_env_idx] = 0;