3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <fdt_support.h>
11 #include <asm/addrspace.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 #define LINUX_MAX_ENVS 256
16 #define LINUX_MAX_ARGS 256
18 #if defined(CONFIG_MALTA)
19 #define mips_boot_malta 1
21 #define mips_boot_malta 0
24 #if defined(CONFIG_MIPS_BOOT_CMDLINE_LEGACY)
25 #define mips_boot_cmdline_legacy 1
27 #define mips_boot_cmdline_legacy 0
30 #if defined(CONFIG_MIPS_BOOT_ENV_LEGACY)
31 #define mips_boot_env_legacy 1
33 #define mips_boot_env_legacy 0
36 static int linux_argc;
37 static char **linux_argv;
38 static char *linux_argp;
40 static char **linux_env;
41 static char *linux_env_p;
42 static int linux_env_idx;
44 static ulong arch_get_sp(void)
48 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
53 void arch_lmb_reserve(struct lmb *lmb)
58 debug("## Current stack ends at 0x%08lx\n", sp);
60 /* adjust sp by 4K to be safe */
62 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
65 static int boot_setup_linux(bootm_headers_t *images)
70 rd_len = images->rd_end - images->rd_start;
71 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
72 rd_len, &images->initrd_start, &images->initrd_end);
76 #if defined(CONFIG_MIPS_BOOT_FDT) && defined(CONFIG_OF_LIBFDT)
78 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
80 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
90 static void boot_setup_fdt(bootm_headers_t *images)
92 #if defined(CONFIG_MIPS_BOOT_FDT) && defined(CONFIG_OF_LIBFDT)
94 u64 mem_size = gd->ram_size;
96 debug("## setup FDT\n");
98 fdt_chosen(images->ft_addr, 1);
99 fdt_fixup_memory_banks(images->ft_addr, &mem_start, &mem_size, 1);
100 fdt_fixup_ethernet(images->ft_addr);
101 fdt_initrd(images->ft_addr, images->initrd_start, images->initrd_end, 1);
103 #if defined(CONFIG_OF_BOARD_SETUP)
104 ft_board_setup(images->ft_addr, gd->bd);
109 static void linux_cmdline_init(void)
112 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
114 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
117 static void linux_cmdline_set(const char *value, size_t len)
119 linux_argv[linux_argc] = linux_argp;
120 memcpy(linux_argp, value, len);
123 linux_argp += len + 1;
127 static void linux_cmdline_dump(void)
131 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
132 linux_argv, linux_argp);
134 for (i = 1; i < linux_argc; i++)
135 debug(" arg %03d: %s\n", i, linux_argv[i]);
138 static void linux_cmdline_legacy(bootm_headers_t *images)
140 const char *bootargs, *next, *quote;
142 linux_cmdline_init();
144 bootargs = getenv("bootargs");
150 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
151 quote = strchr(bootargs, '"');
152 next = strchr(bootargs, ' ');
154 while (next && quote && quote < next) {
156 * we found a left quote before the next blank
157 * now we have to find the matching right quote
159 next = strchr(quote + 1, '"');
161 quote = strchr(next + 1, '"');
162 next = strchr(next + 1, ' ');
167 next = bootargs + strlen(bootargs);
169 linux_cmdline_set(bootargs, next - bootargs);
178 static void linux_cmdline_append(bootm_headers_t *images)
181 ulong mem, rd_start, rd_size;
184 mem = gd->ram_size >> 20;
185 sprintf(buf, "mem=%luM", mem);
186 linux_cmdline_set(buf, strlen(buf));
188 /* append rd_start and rd_size */
189 rd_start = images->initrd_start;
190 rd_size = images->initrd_end - images->initrd_start;
193 sprintf(buf, "rd_start=0x%08lX", rd_start);
194 linux_cmdline_set(buf, strlen(buf));
195 sprintf(buf, "rd_size=0x%lX", rd_size);
196 linux_cmdline_set(buf, strlen(buf));
200 static void boot_cmdline_linux(bootm_headers_t *images)
202 if (mips_boot_cmdline_legacy && !images->ft_len) {
203 linux_cmdline_legacy(images);
205 if (!mips_boot_env_legacy)
206 linux_cmdline_append(images);
208 linux_cmdline_dump();
212 static void linux_env_init(void)
214 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
216 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
220 static void linux_env_set(const char *env_name, const char *env_val)
222 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
223 linux_env[linux_env_idx] = linux_env_p;
225 strcpy(linux_env_p, env_name);
226 linux_env_p += strlen(env_name);
228 if (mips_boot_malta) {
230 linux_env[++linux_env_idx] = linux_env_p;
232 *linux_env_p++ = '=';
235 strcpy(linux_env_p, env_val);
236 linux_env_p += strlen(env_val);
239 linux_env[++linux_env_idx] = 0;
243 static void linux_env_legacy(bootm_headers_t *images)
247 ulong rd_start, rd_size;
249 #ifdef CONFIG_MEMSIZE_IN_BYTES
250 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
251 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
253 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
254 debug("## Giving linux memsize in MB, %lu\n",
255 (ulong)(gd->ram_size >> 20));
256 #endif /* CONFIG_MEMSIZE_IN_BYTES */
258 rd_start = UNCACHED_SDRAM(images->initrd_start);
259 rd_size = images->initrd_end - images->initrd_start;
263 linux_env_set("memsize", env_buf);
265 sprintf(env_buf, "0x%08lX", rd_start);
266 linux_env_set("initrd_start", env_buf);
268 sprintf(env_buf, "0x%lX", rd_size);
269 linux_env_set("initrd_size", env_buf);
271 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
272 linux_env_set("flash_start", env_buf);
274 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
275 linux_env_set("flash_size", env_buf);
277 cp = getenv("ethaddr");
279 linux_env_set("ethaddr", cp);
281 cp = getenv("eth1addr");
283 linux_env_set("eth1addr", cp);
285 if (mips_boot_malta) {
286 sprintf(env_buf, "%un8r", gd->baudrate);
287 linux_env_set("modetty0", env_buf);
291 static void boot_prep_linux(bootm_headers_t *images)
293 if (mips_boot_env_legacy && !images->ft_len)
294 linux_env_legacy(images);
297 boot_setup_fdt(images);
300 static void boot_jump_linux(bootm_headers_t *images)
302 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
303 kernel_entry_t kernel = (kernel_entry_t) images->ep;
304 ulong linux_extra = 0;
306 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
308 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
311 linux_extra = gd->ram_size;
313 #ifdef CONFIG_BOOTSTAGE_FDT
314 bootstage_fdt_add_report();
316 #ifdef CONFIG_BOOTSTAGE_REPORT
320 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
323 int do_bootm_linux(int flag, int argc, char * const argv[],
324 bootm_headers_t *images)
328 /* No need for those on MIPS */
329 if (flag & BOOTM_STATE_OS_BD_T)
332 if (flag & BOOTM_STATE_OS_CMDLINE) {
333 boot_cmdline_linux(images);
337 if (flag & BOOTM_STATE_OS_PREP) {
338 boot_prep_linux(images);
342 if (flag & BOOTM_STATE_OS_GO) {
343 boot_jump_linux(images);
347 ret = boot_setup_linux(images);
351 boot_cmdline_linux(images);
352 boot_prep_linux(images);
353 boot_jump_linux(images);
355 /* does not return */