1 // SPDX-License-Identifier: GPL-2.0+
11 #include <fdt_support.h>
14 #include <asm/addrspace.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 #define LINUX_MAX_ENVS 256
20 #define LINUX_MAX_ARGS 256
22 static int linux_argc;
23 static char **linux_argv;
24 static char *linux_argp;
26 static char **linux_env;
27 static char *linux_env_p;
28 static int linux_env_idx;
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, gd->ram_top - sp);
51 static void linux_cmdline_init(void)
54 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
56 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
59 static void linux_cmdline_set(const char *value, size_t len)
61 linux_argv[linux_argc] = linux_argp;
62 memcpy(linux_argp, value, len);
65 linux_argp += len + 1;
69 static void linux_cmdline_dump(void)
73 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
74 linux_argv, linux_argp);
76 for (i = 1; i < linux_argc; i++)
77 debug(" arg %03d: %s\n", i, linux_argv[i]);
80 static void linux_cmdline_legacy(bootm_headers_t *images)
82 const char *bootargs, *next, *quote;
86 bootargs = env_get("bootargs");
92 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
93 quote = strchr(bootargs, '"');
94 next = strchr(bootargs, ' ');
96 while (next && quote && quote < next) {
98 * we found a left quote before the next blank
99 * now we have to find the matching right quote
101 next = strchr(quote + 1, '"');
103 quote = strchr(next + 1, '"');
104 next = strchr(next + 1, ' ');
109 next = bootargs + strlen(bootargs);
111 linux_cmdline_set(bootargs, next - bootargs);
120 static void linux_cmdline_append(bootm_headers_t *images)
123 ulong mem, rd_start, rd_size;
126 mem = gd->ram_size >> 20;
127 sprintf(buf, "mem=%luM", mem);
128 linux_cmdline_set(buf, strlen(buf));
130 /* append rd_start and rd_size */
131 rd_start = images->initrd_start;
132 rd_size = images->initrd_end - images->initrd_start;
135 sprintf(buf, "rd_start=0x%08lX", rd_start);
136 linux_cmdline_set(buf, strlen(buf));
137 sprintf(buf, "rd_size=0x%lX", rd_size);
138 linux_cmdline_set(buf, strlen(buf));
142 static void linux_env_init(void)
144 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
146 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
150 static void linux_env_set(const char *env_name, const char *env_val)
152 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
153 linux_env[linux_env_idx] = linux_env_p;
155 strcpy(linux_env_p, env_name);
156 linux_env_p += strlen(env_name);
158 if (CONFIG_IS_ENABLED(MALTA)) {
160 linux_env[++linux_env_idx] = linux_env_p;
162 *linux_env_p++ = '=';
165 strcpy(linux_env_p, env_val);
166 linux_env_p += strlen(env_val);
169 linux_env[++linux_env_idx] = 0;
173 static void linux_env_legacy(bootm_headers_t *images)
177 ulong rd_start, rd_size;
179 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
180 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
181 debug("## Giving linux memsize in bytes, %lu\n",
182 (ulong)gd->ram_size);
184 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
185 debug("## Giving linux memsize in MB, %lu\n",
186 (ulong)(gd->ram_size >> 20));
189 rd_start = UNCACHED_SDRAM(images->initrd_start);
190 rd_size = images->initrd_end - images->initrd_start;
194 linux_env_set("memsize", env_buf);
196 sprintf(env_buf, "0x%08lX", rd_start);
197 linux_env_set("initrd_start", env_buf);
199 sprintf(env_buf, "0x%lX", rd_size);
200 linux_env_set("initrd_size", env_buf);
202 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
203 linux_env_set("flash_start", env_buf);
205 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
206 linux_env_set("flash_size", env_buf);
208 cp = env_get("ethaddr");
210 linux_env_set("ethaddr", cp);
212 cp = env_get("eth1addr");
214 linux_env_set("eth1addr", cp);
216 if (CONFIG_IS_ENABLED(MALTA)) {
217 sprintf(env_buf, "%un8r", gd->baudrate);
218 linux_env_set("modetty0", env_buf);
222 static int boot_reloc_fdt(bootm_headers_t *images)
225 * In case of legacy uImage's, relocation of FDT is already done
226 * by do_bootm_states() and should not repeated in 'bootm prep'.
228 if (images->state & BOOTM_STATE_FDT) {
229 debug("## FDT already relocated\n");
233 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
234 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
235 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
242 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
243 int arch_fixup_fdt(void *blob)
245 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
246 u64 mem_size = gd->ram_size;
248 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
252 static int boot_setup_fdt(bootm_headers_t *images)
254 images->initrd_start = virt_to_phys((void *)images->initrd_start);
255 images->initrd_end = virt_to_phys((void *)images->initrd_end);
256 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
260 static void boot_prep_linux(bootm_headers_t *images)
262 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
263 boot_reloc_fdt(images);
264 boot_setup_fdt(images);
266 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
267 linux_cmdline_legacy(images);
269 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
270 linux_cmdline_append(images);
272 linux_cmdline_dump();
275 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
276 linux_env_legacy(images);
280 static void boot_jump_linux(bootm_headers_t *images)
282 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
283 kernel_entry_t kernel = (kernel_entry_t) images->ep;
284 ulong linux_extra = 0;
286 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
288 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
290 if (CONFIG_IS_ENABLED(MALTA))
291 linux_extra = gd->ram_size;
293 #if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
294 bootstage_fdt_add_report();
296 #if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
300 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
304 kernel(-2, (ulong)images->ft_addr, 0, 0);
306 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
310 int do_bootm_linux(int flag, int argc, char *const argv[],
311 bootm_headers_t *images)
313 /* No need for those on MIPS */
314 if (flag & BOOTM_STATE_OS_BD_T)
318 * Cmdline init has been moved to 'bootm prep' because it has to be
319 * done after relocation of ramdisk to always pass correct values
320 * for rd_start and rd_size to Linux kernel.
322 if (flag & BOOTM_STATE_OS_CMDLINE)
325 if (flag & BOOTM_STATE_OS_PREP) {
326 boot_prep_linux(images);
330 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
331 boot_jump_linux(images);
335 /* does not return */