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)
42 arch_lmb_reserve_generic(lmb, arch_get_sp(), gd->ram_top, 4096);
45 static void linux_cmdline_init(void)
48 linux_argv = (char **)CKSEG1ADDR(gd->bd->bi_boot_params);
50 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
53 static void linux_cmdline_set(const char *value, size_t len)
55 linux_argv[linux_argc] = linux_argp;
56 memcpy(linux_argp, value, len);
59 linux_argp += len + 1;
63 static void linux_cmdline_dump(void)
67 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
68 linux_argv, linux_argp);
70 for (i = 1; i < linux_argc; i++)
71 debug(" arg %03d: %s\n", i, linux_argv[i]);
74 static void linux_cmdline_legacy(bootm_headers_t *images)
76 const char *bootargs, *next, *quote;
80 bootargs = env_get("bootargs");
86 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
87 quote = strchr(bootargs, '"');
88 next = strchr(bootargs, ' ');
90 while (next && quote && quote < next) {
92 * we found a left quote before the next blank
93 * now we have to find the matching right quote
95 next = strchr(quote + 1, '"');
97 quote = strchr(next + 1, '"');
98 next = strchr(next + 1, ' ');
103 next = bootargs + strlen(bootargs);
105 linux_cmdline_set(bootargs, next - bootargs);
114 static void linux_cmdline_append(bootm_headers_t *images)
117 ulong mem, rd_start, rd_size;
120 mem = gd->ram_size >> 20;
121 sprintf(buf, "mem=%luM", mem);
122 linux_cmdline_set(buf, strlen(buf));
124 /* append rd_start and rd_size */
125 rd_start = images->initrd_start;
126 rd_size = images->initrd_end - images->initrd_start;
129 sprintf(buf, "rd_start=0x%08lX", rd_start);
130 linux_cmdline_set(buf, strlen(buf));
131 sprintf(buf, "rd_size=0x%lX", rd_size);
132 linux_cmdline_set(buf, strlen(buf));
136 static void linux_env_init(void)
138 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
140 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
144 static void linux_env_set(const char *env_name, const char *env_val)
146 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
147 linux_env[linux_env_idx] = linux_env_p;
149 strcpy(linux_env_p, env_name);
150 linux_env_p += strlen(env_name);
152 if (CONFIG_IS_ENABLED(MALTA)) {
154 linux_env[++linux_env_idx] = linux_env_p;
156 *linux_env_p++ = '=';
159 strcpy(linux_env_p, env_val);
160 linux_env_p += strlen(env_val);
163 linux_env[++linux_env_idx] = 0;
167 static void linux_env_legacy(bootm_headers_t *images)
171 ulong rd_start, rd_size;
173 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
174 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
175 debug("## Giving linux memsize in bytes, %lu\n",
176 (ulong)gd->ram_size);
178 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
179 debug("## Giving linux memsize in MB, %lu\n",
180 (ulong)(gd->ram_size >> 20));
183 rd_start = CKSEG1ADDR(images->initrd_start);
184 rd_size = images->initrd_end - images->initrd_start;
188 linux_env_set("memsize", env_buf);
190 sprintf(env_buf, "0x%08lX", rd_start);
191 linux_env_set("initrd_start", env_buf);
193 sprintf(env_buf, "0x%lX", rd_size);
194 linux_env_set("initrd_size", env_buf);
196 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
197 linux_env_set("flash_start", env_buf);
199 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
200 linux_env_set("flash_size", env_buf);
202 cp = env_get("ethaddr");
204 linux_env_set("ethaddr", cp);
206 cp = env_get("eth1addr");
208 linux_env_set("eth1addr", cp);
210 if (CONFIG_IS_ENABLED(MALTA)) {
211 sprintf(env_buf, "%un8r", gd->baudrate);
212 linux_env_set("modetty0", env_buf);
216 static int boot_reloc_fdt(bootm_headers_t *images)
219 * In case of legacy uImage's, relocation of FDT is already done
220 * by do_bootm_states() and should not repeated in 'bootm prep'.
222 if (images->state & BOOTM_STATE_FDT) {
223 debug("## FDT already relocated\n");
227 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
228 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
229 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
236 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
237 int arch_fixup_fdt(void *blob)
239 u64 mem_start = virt_to_phys((void *)gd->ram_base);
240 u64 mem_size = gd->ram_size;
242 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
246 static int boot_setup_fdt(bootm_headers_t *images)
248 images->initrd_start = virt_to_phys((void *)images->initrd_start);
249 images->initrd_end = virt_to_phys((void *)images->initrd_end);
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)
294 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
298 kernel(-2, (ulong)images->ft_addr, 0, 0);
300 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
304 int do_bootm_linux(int flag, int argc, char *const argv[],
305 bootm_headers_t *images)
307 /* No need for those on MIPS */
308 if (flag & BOOTM_STATE_OS_BD_T)
312 * Cmdline init has been moved to 'bootm prep' because it has to be
313 * done after relocation of ramdisk to always pass correct values
314 * for rd_start and rd_size to Linux kernel.
316 if (flag & BOOTM_STATE_OS_CMDLINE)
319 if (flag & BOOTM_STATE_OS_PREP) {
320 boot_prep_linux(images);
324 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
325 boot_jump_linux(images);
329 /* does not return */