1 // SPDX-License-Identifier: GPL-2.0+
11 #include <fdt_support.h>
14 #include <asm/addrspace.h>
15 #include <asm/global_data.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 #define LINUX_MAX_ENVS 256
21 #define LINUX_MAX_ARGS 256
23 static int linux_argc;
24 static char **linux_argv;
25 static char *linux_argp;
27 static char **linux_env;
28 static char *linux_env_p;
29 static int linux_env_idx;
31 static ulong arch_get_sp(void)
35 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
40 void arch_lmb_reserve(struct lmb *lmb)
45 debug("## Current stack ends at 0x%08lx\n", sp);
47 /* adjust sp by 4K to be safe */
49 lmb_reserve(lmb, sp, gd->ram_top - sp);
52 static void linux_cmdline_init(void)
55 linux_argv = (char **)CKSEG1ADDR(gd->bd->bi_boot_params);
57 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
60 static void linux_cmdline_set(const char *value, size_t len)
62 linux_argv[linux_argc] = linux_argp;
63 memcpy(linux_argp, value, len);
66 linux_argp += len + 1;
70 static void linux_cmdline_dump(void)
74 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
75 linux_argv, linux_argp);
77 for (i = 1; i < linux_argc; i++)
78 debug(" arg %03d: %s\n", i, linux_argv[i]);
81 static void linux_cmdline_legacy(bootm_headers_t *images)
83 const char *bootargs, *next, *quote;
87 bootargs = env_get("bootargs");
93 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
94 quote = strchr(bootargs, '"');
95 next = strchr(bootargs, ' ');
97 while (next && quote && quote < next) {
99 * we found a left quote before the next blank
100 * now we have to find the matching right quote
102 next = strchr(quote + 1, '"');
104 quote = strchr(next + 1, '"');
105 next = strchr(next + 1, ' ');
110 next = bootargs + strlen(bootargs);
112 linux_cmdline_set(bootargs, next - bootargs);
121 static void linux_cmdline_append(bootm_headers_t *images)
124 ulong mem, rd_start, rd_size;
127 mem = gd->ram_size >> 20;
128 sprintf(buf, "mem=%luM", mem);
129 linux_cmdline_set(buf, strlen(buf));
131 /* append rd_start and rd_size */
132 rd_start = images->initrd_start;
133 rd_size = images->initrd_end - images->initrd_start;
136 sprintf(buf, "rd_start=0x%08lX", rd_start);
137 linux_cmdline_set(buf, strlen(buf));
138 sprintf(buf, "rd_size=0x%lX", rd_size);
139 linux_cmdline_set(buf, strlen(buf));
143 static void linux_env_init(void)
145 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
147 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
151 static void linux_env_set(const char *env_name, const char *env_val)
153 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
154 linux_env[linux_env_idx] = linux_env_p;
156 strcpy(linux_env_p, env_name);
157 linux_env_p += strlen(env_name);
159 if (CONFIG_IS_ENABLED(MALTA)) {
161 linux_env[++linux_env_idx] = linux_env_p;
163 *linux_env_p++ = '=';
166 strcpy(linux_env_p, env_val);
167 linux_env_p += strlen(env_val);
170 linux_env[++linux_env_idx] = 0;
174 static void linux_env_legacy(bootm_headers_t *images)
178 ulong rd_start, rd_size;
180 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
181 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
182 debug("## Giving linux memsize in bytes, %lu\n",
183 (ulong)gd->ram_size);
185 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
186 debug("## Giving linux memsize in MB, %lu\n",
187 (ulong)(gd->ram_size >> 20));
190 rd_start = CKSEG1ADDR(images->initrd_start);
191 rd_size = images->initrd_end - images->initrd_start;
195 linux_env_set("memsize", env_buf);
197 sprintf(env_buf, "0x%08lX", rd_start);
198 linux_env_set("initrd_start", env_buf);
200 sprintf(env_buf, "0x%lX", rd_size);
201 linux_env_set("initrd_size", env_buf);
203 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
204 linux_env_set("flash_start", env_buf);
206 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
207 linux_env_set("flash_size", env_buf);
209 cp = env_get("ethaddr");
211 linux_env_set("ethaddr", cp);
213 cp = env_get("eth1addr");
215 linux_env_set("eth1addr", cp);
217 if (CONFIG_IS_ENABLED(MALTA)) {
218 sprintf(env_buf, "%un8r", gd->baudrate);
219 linux_env_set("modetty0", env_buf);
223 static int boot_reloc_fdt(bootm_headers_t *images)
226 * In case of legacy uImage's, relocation of FDT is already done
227 * by do_bootm_states() and should not repeated in 'bootm prep'.
229 if (images->state & BOOTM_STATE_FDT) {
230 debug("## FDT already relocated\n");
234 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
235 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
236 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
243 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
244 int arch_fixup_fdt(void *blob)
246 u64 mem_start = virt_to_phys((void *)gd->ram_base);
247 u64 mem_size = gd->ram_size;
249 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
253 static int boot_setup_fdt(bootm_headers_t *images)
255 images->initrd_start = virt_to_phys((void *)images->initrd_start);
256 images->initrd_end = virt_to_phys((void *)images->initrd_end);
257 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
261 static void boot_prep_linux(bootm_headers_t *images)
263 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
264 boot_reloc_fdt(images);
265 boot_setup_fdt(images);
267 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
268 linux_cmdline_legacy(images);
270 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
271 linux_cmdline_append(images);
273 linux_cmdline_dump();
276 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
277 linux_env_legacy(images);
281 static void boot_jump_linux(bootm_headers_t *images)
283 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
284 kernel_entry_t kernel = (kernel_entry_t) images->ep;
285 ulong linux_extra = 0;
287 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
289 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
291 if (CONFIG_IS_ENABLED(MALTA))
292 linux_extra = gd->ram_size;
294 #if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
295 bootstage_fdt_add_report();
297 #if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
301 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
305 kernel(-2, (ulong)images->ft_addr, 0, 0);
307 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
311 int do_bootm_linux(int flag, int argc, char *const argv[],
312 bootm_headers_t *images)
314 /* No need for those on MIPS */
315 if (flag & BOOTM_STATE_OS_BD_T)
319 * Cmdline init has been moved to 'bootm prep' because it has to be
320 * done after relocation of ramdisk to always pass correct values
321 * for rd_start and rd_size to Linux kernel.
323 if (flag & BOOTM_STATE_OS_CMDLINE)
326 if (flag & BOOTM_STATE_OS_PREP) {
327 boot_prep_linux(images);
331 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
332 boot_jump_linux(images);
336 /* does not return */