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;
23 static char **linux_env;
24 static char *linux_env_p;
25 static int linux_env_idx;
27 static void linux_params_init(ulong start, char *commandline);
28 static void linux_env_set(char *env_name, char *env_val);
30 static ulong arch_get_sp(void)
34 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
39 void arch_lmb_reserve(struct lmb *lmb)
44 debug("## Current stack ends at 0x%08lx\n", sp);
46 /* adjust sp by 4K to be safe */
48 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
51 static void boot_prep_linux(bootm_headers_t *images)
53 char *commandline = getenv("bootargs");
57 linux_params_init(UNCACHED_SDRAM(gd->bd->bi_boot_params), commandline);
59 #ifdef CONFIG_MEMSIZE_IN_BYTES
60 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
61 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
63 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
64 debug("## Giving linux memsize in MB, %lu\n",
65 (ulong)(gd->ram_size >> 20));
66 #endif /* CONFIG_MEMSIZE_IN_BYTES */
68 linux_env_set("memsize", env_buf);
70 sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start));
71 linux_env_set("initrd_start", env_buf);
73 sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start));
74 linux_env_set("initrd_size", env_buf);
76 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
77 linux_env_set("flash_start", env_buf);
79 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
80 linux_env_set("flash_size", env_buf);
82 cp = getenv("ethaddr");
84 linux_env_set("ethaddr", cp);
86 cp = getenv("eth1addr");
88 linux_env_set("eth1addr", cp);
91 static void boot_jump_linux(bootm_headers_t *images)
93 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
94 kernel_entry_t kernel = (kernel_entry_t) images->ep;
96 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
98 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
100 /* we assume that the kernel is in place */
101 printf("\nStarting kernel ...\n\n");
103 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0);
106 int do_bootm_linux(int flag, int argc, char * const argv[],
107 bootm_headers_t *images)
109 /* No need for those on MIPS */
110 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
113 if (flag & BOOTM_STATE_OS_PREP) {
114 boot_prep_linux(images);
118 if (flag & BOOTM_STATE_OS_GO) {
119 boot_jump_linux(images);
123 boot_prep_linux(images);
124 boot_jump_linux(images);
126 /* does not return */
130 static void linux_params_init(ulong start, char *line)
132 char *next, *quote, *argp;
135 linux_argv = (char **)start;
137 argp = (char *)(linux_argv + LINUX_MAX_ARGS);
141 while (line && *line && linux_argc < LINUX_MAX_ARGS) {
142 quote = strchr(line, '"');
143 next = strchr(line, ' ');
145 while (next && quote && quote < next) {
147 * we found a left quote before the next blank
148 * now we have to find the matching right quote
150 next = strchr(quote + 1, '"');
152 quote = strchr(next + 1, '"');
153 next = strchr(next + 1, ' ');
158 next = line + strlen(line);
160 linux_argv[linux_argc] = argp;
161 memcpy(argp, line, next - line);
162 argp[next - line] = 0;
164 argp += next - line + 1;
173 linux_env = (char **)(((ulong) argp + 15) & ~15);
175 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
179 static void linux_env_set(char *env_name, char *env_val)
181 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
182 linux_env[linux_env_idx] = linux_env_p;
184 strcpy(linux_env_p, env_name);
185 linux_env_p += strlen(env_name);
187 strcpy(linux_env_p, "=");
190 strcpy(linux_env_p, env_val);
191 linux_env_p += strlen(env_val);
194 linux_env[++linux_env_idx] = 0;