1 // SPDX-License-Identifier: GPL-2.0+
9 #include <fdt_support.h>
10 #include <asm/addrspace.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 #define LINUX_MAX_ENVS 256
16 #define LINUX_MAX_ARGS 256
18 static int linux_argc;
19 static char **linux_argv;
20 static char *linux_argp;
22 static char **linux_env;
23 static char *linux_env_p;
24 static int linux_env_idx;
26 static ulong arch_get_sp(void)
30 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
35 void arch_lmb_reserve(struct lmb *lmb)
40 debug("## Current stack ends at 0x%08lx\n", sp);
42 /* adjust sp by 4K to be safe */
44 lmb_reserve(lmb, sp, gd->ram_top - sp);
47 static void linux_cmdline_init(void)
50 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
52 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
55 static void linux_cmdline_set(const char *value, size_t len)
57 linux_argv[linux_argc] = linux_argp;
58 memcpy(linux_argp, value, len);
61 linux_argp += len + 1;
65 static void linux_cmdline_dump(void)
69 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
70 linux_argv, linux_argp);
72 for (i = 1; i < linux_argc; i++)
73 debug(" arg %03d: %s\n", i, linux_argv[i]);
76 static void linux_cmdline_legacy(bootm_headers_t *images)
78 const char *bootargs, *next, *quote;
82 bootargs = env_get("bootargs");
88 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
89 quote = strchr(bootargs, '"');
90 next = strchr(bootargs, ' ');
92 while (next && quote && quote < next) {
94 * we found a left quote before the next blank
95 * now we have to find the matching right quote
97 next = strchr(quote + 1, '"');
99 quote = strchr(next + 1, '"');
100 next = strchr(next + 1, ' ');
105 next = bootargs + strlen(bootargs);
107 linux_cmdline_set(bootargs, next - bootargs);
116 static void linux_cmdline_append(bootm_headers_t *images)
119 ulong mem, rd_start, rd_size;
122 mem = gd->ram_size >> 20;
123 sprintf(buf, "mem=%luM", mem);
124 linux_cmdline_set(buf, strlen(buf));
126 /* append rd_start and rd_size */
127 rd_start = images->initrd_start;
128 rd_size = images->initrd_end - images->initrd_start;
131 sprintf(buf, "rd_start=0x%08lX", rd_start);
132 linux_cmdline_set(buf, strlen(buf));
133 sprintf(buf, "rd_size=0x%lX", rd_size);
134 linux_cmdline_set(buf, strlen(buf));
138 static void linux_env_init(void)
140 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
142 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
146 static void linux_env_set(const char *env_name, const char *env_val)
148 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
149 linux_env[linux_env_idx] = linux_env_p;
151 strcpy(linux_env_p, env_name);
152 linux_env_p += strlen(env_name);
154 if (CONFIG_IS_ENABLED(MALTA)) {
156 linux_env[++linux_env_idx] = linux_env_p;
158 *linux_env_p++ = '=';
161 strcpy(linux_env_p, env_val);
162 linux_env_p += strlen(env_val);
165 linux_env[++linux_env_idx] = 0;
169 static void linux_env_legacy(bootm_headers_t *images)
173 ulong rd_start, rd_size;
175 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
176 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
177 debug("## Giving linux memsize in bytes, %lu\n",
178 (ulong)gd->ram_size);
180 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
181 debug("## Giving linux memsize in MB, %lu\n",
182 (ulong)(gd->ram_size >> 20));
185 rd_start = UNCACHED_SDRAM(images->initrd_start);
186 rd_size = images->initrd_end - images->initrd_start;
190 linux_env_set("memsize", env_buf);
192 sprintf(env_buf, "0x%08lX", rd_start);
193 linux_env_set("initrd_start", env_buf);
195 sprintf(env_buf, "0x%lX", rd_size);
196 linux_env_set("initrd_size", env_buf);
198 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
199 linux_env_set("flash_start", env_buf);
201 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
202 linux_env_set("flash_size", env_buf);
204 cp = env_get("ethaddr");
206 linux_env_set("ethaddr", cp);
208 cp = env_get("eth1addr");
210 linux_env_set("eth1addr", cp);
212 if (CONFIG_IS_ENABLED(MALTA)) {
213 sprintf(env_buf, "%un8r", gd->baudrate);
214 linux_env_set("modetty0", env_buf);
218 static int boot_reloc_fdt(bootm_headers_t *images)
221 * In case of legacy uImage's, relocation of FDT is already done
222 * by do_bootm_states() and should not repeated in 'bootm prep'.
224 if (images->state & BOOTM_STATE_FDT) {
225 debug("## FDT already relocated\n");
229 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
230 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
231 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
238 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
239 int arch_fixup_fdt(void *blob)
241 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
242 u64 mem_size = gd->ram_size;
244 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
248 static int boot_setup_fdt(bootm_headers_t *images)
250 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
254 static void boot_prep_linux(bootm_headers_t *images)
256 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
257 boot_reloc_fdt(images);
258 boot_setup_fdt(images);
260 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
261 linux_cmdline_legacy(images);
263 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
264 linux_cmdline_append(images);
266 linux_cmdline_dump();
269 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
270 linux_env_legacy(images);
274 static void boot_jump_linux(bootm_headers_t *images)
276 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
277 kernel_entry_t kernel = (kernel_entry_t) images->ep;
278 ulong linux_extra = 0;
280 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
282 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
284 if (CONFIG_IS_ENABLED(MALTA))
285 linux_extra = gd->ram_size;
287 #if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
288 bootstage_fdt_add_report();
290 #if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
295 kernel(-2, (ulong)images->ft_addr, 0, 0);
297 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
301 int do_bootm_linux(int flag, int argc, char * const argv[],
302 bootm_headers_t *images)
304 /* No need for those on MIPS */
305 if (flag & BOOTM_STATE_OS_BD_T)
309 * Cmdline init has been moved to 'bootm prep' because it has to be
310 * done after relocation of ramdisk to always pass correct values
311 * for rd_start and rd_size to Linux kernel.
313 if (flag & BOOTM_STATE_OS_CMDLINE)
316 if (flag & BOOTM_STATE_OS_PREP) {
317 boot_prep_linux(images);
321 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
322 boot_jump_linux(images);
326 /* does not return */