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 void boot_prep_linux(bootm_headers_t *images)
32 char *commandline = getenv("bootargs");
36 linux_params_init(UNCACHED_SDRAM(gd->bd->bi_boot_params), commandline);
38 #ifdef CONFIG_MEMSIZE_IN_BYTES
39 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
40 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
42 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
43 debug("## Giving linux memsize in MB, %lu\n",
44 (ulong)(gd->ram_size >> 20));
45 #endif /* CONFIG_MEMSIZE_IN_BYTES */
47 linux_env_set("memsize", env_buf);
49 sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start));
50 linux_env_set("initrd_start", env_buf);
52 sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start));
53 linux_env_set("initrd_size", env_buf);
55 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
56 linux_env_set("flash_start", env_buf);
58 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
59 linux_env_set("flash_size", env_buf);
61 cp = getenv("ethaddr");
63 linux_env_set("ethaddr", cp);
65 cp = getenv("eth1addr");
67 linux_env_set("eth1addr", cp);
70 static void boot_jump_linux(bootm_headers_t *images)
72 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
73 kernel_entry_t kernel = (kernel_entry_t) images->ep;
75 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
77 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
79 /* we assume that the kernel is in place */
80 printf("\nStarting kernel ...\n\n");
82 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0);
85 int do_bootm_linux(int flag, int argc, char * const argv[],
86 bootm_headers_t *images)
88 /* No need for those on MIPS */
89 if (flag & BOOTM_STATE_OS_BD_T || flag & BOOTM_STATE_OS_CMDLINE)
92 if (flag & BOOTM_STATE_OS_PREP) {
93 boot_prep_linux(images);
97 if (flag & BOOTM_STATE_OS_GO) {
98 boot_jump_linux(images);
102 boot_prep_linux(images);
103 boot_jump_linux(images);
105 /* does not return */
109 static void linux_params_init(ulong start, char *line)
111 char *next, *quote, *argp;
114 linux_argv = (char **)start;
116 argp = (char *)(linux_argv + LINUX_MAX_ARGS);
120 while (line && *line && linux_argc < LINUX_MAX_ARGS) {
121 quote = strchr(line, '"');
122 next = strchr(line, ' ');
124 while (next && quote && quote < next) {
126 * we found a left quote before the next blank
127 * now we have to find the matching right quote
129 next = strchr(quote + 1, '"');
131 quote = strchr(next + 1, '"');
132 next = strchr(next + 1, ' ');
137 next = line + strlen(line);
139 linux_argv[linux_argc] = argp;
140 memcpy(argp, line, next - line);
141 argp[next - line] = 0;
143 argp += next - line + 1;
152 linux_env = (char **)(((ulong) argp + 15) & ~15);
154 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
158 static void linux_env_set(char *env_name, char *env_val)
160 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
161 linux_env[linux_env_idx] = linux_env_p;
163 strcpy(linux_env_p, env_name);
164 linux_env_p += strlen(env_name);
166 strcpy(linux_env_p, "=");
169 strcpy(linux_env_p, env_val);
170 linux_env_p += strlen(env_val);
173 linux_env[++linux_env_idx] = 0;