1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 #include <fdt_support.h>
12 #include <asm/addrspace.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 #define LINUX_MAX_ENVS 256
18 #define LINUX_MAX_ARGS 256
20 static int linux_argc;
21 static char **linux_argv;
22 static char *linux_argp;
24 static char **linux_env;
25 static char *linux_env_p;
26 static int linux_env_idx;
28 static ulong arch_get_sp(void)
32 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
37 void arch_lmb_reserve(struct lmb *lmb)
42 debug("## Current stack ends at 0x%08lx\n", sp);
44 /* adjust sp by 4K to be safe */
46 lmb_reserve(lmb, sp, gd->ram_top - sp);
49 static void linux_cmdline_init(void)
52 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
54 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
57 static void linux_cmdline_set(const char *value, size_t len)
59 linux_argv[linux_argc] = linux_argp;
60 memcpy(linux_argp, value, len);
63 linux_argp += len + 1;
67 static void linux_cmdline_dump(void)
71 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
72 linux_argv, linux_argp);
74 for (i = 1; i < linux_argc; i++)
75 debug(" arg %03d: %s\n", i, linux_argv[i]);
78 static void linux_cmdline_legacy(bootm_headers_t *images)
80 const char *bootargs, *next, *quote;
84 bootargs = env_get("bootargs");
90 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
91 quote = strchr(bootargs, '"');
92 next = strchr(bootargs, ' ');
94 while (next && quote && quote < next) {
96 * we found a left quote before the next blank
97 * now we have to find the matching right quote
99 next = strchr(quote + 1, '"');
101 quote = strchr(next + 1, '"');
102 next = strchr(next + 1, ' ');
107 next = bootargs + strlen(bootargs);
109 linux_cmdline_set(bootargs, next - bootargs);
118 static void linux_cmdline_append(bootm_headers_t *images)
121 ulong mem, rd_start, rd_size;
124 mem = gd->ram_size >> 20;
125 sprintf(buf, "mem=%luM", mem);
126 linux_cmdline_set(buf, strlen(buf));
128 /* append rd_start and rd_size */
129 rd_start = images->initrd_start;
130 rd_size = images->initrd_end - images->initrd_start;
133 sprintf(buf, "rd_start=0x%08lX", rd_start);
134 linux_cmdline_set(buf, strlen(buf));
135 sprintf(buf, "rd_size=0x%lX", rd_size);
136 linux_cmdline_set(buf, strlen(buf));
140 static void linux_env_init(void)
142 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
144 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
148 static void linux_env_set(const char *env_name, const char *env_val)
150 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
151 linux_env[linux_env_idx] = linux_env_p;
153 strcpy(linux_env_p, env_name);
154 linux_env_p += strlen(env_name);
156 if (CONFIG_IS_ENABLED(MALTA)) {
158 linux_env[++linux_env_idx] = linux_env_p;
160 *linux_env_p++ = '=';
163 strcpy(linux_env_p, env_val);
164 linux_env_p += strlen(env_val);
167 linux_env[++linux_env_idx] = 0;
171 static void linux_env_legacy(bootm_headers_t *images)
175 ulong rd_start, rd_size;
177 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
178 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
179 debug("## Giving linux memsize in bytes, %lu\n",
180 (ulong)gd->ram_size);
182 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
183 debug("## Giving linux memsize in MB, %lu\n",
184 (ulong)(gd->ram_size >> 20));
187 rd_start = UNCACHED_SDRAM(images->initrd_start);
188 rd_size = images->initrd_end - images->initrd_start;
192 linux_env_set("memsize", env_buf);
194 sprintf(env_buf, "0x%08lX", rd_start);
195 linux_env_set("initrd_start", env_buf);
197 sprintf(env_buf, "0x%lX", rd_size);
198 linux_env_set("initrd_size", env_buf);
200 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
201 linux_env_set("flash_start", env_buf);
203 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
204 linux_env_set("flash_size", env_buf);
206 cp = env_get("ethaddr");
208 linux_env_set("ethaddr", cp);
210 cp = env_get("eth1addr");
212 linux_env_set("eth1addr", cp);
214 if (CONFIG_IS_ENABLED(MALTA)) {
215 sprintf(env_buf, "%un8r", gd->baudrate);
216 linux_env_set("modetty0", env_buf);
220 static int boot_reloc_fdt(bootm_headers_t *images)
223 * In case of legacy uImage's, relocation of FDT is already done
224 * by do_bootm_states() and should not repeated in 'bootm prep'.
226 if (images->state & BOOTM_STATE_FDT) {
227 debug("## FDT already relocated\n");
231 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
232 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
233 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
240 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
241 int arch_fixup_fdt(void *blob)
243 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
244 u64 mem_size = gd->ram_size;
246 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
250 static int boot_setup_fdt(bootm_headers_t *images)
252 images->initrd_start = virt_to_phys((void *)images->initrd_start);
253 images->initrd_end = virt_to_phys((void *)images->initrd_end);
254 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
258 static void boot_prep_linux(bootm_headers_t *images)
260 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
261 boot_reloc_fdt(images);
262 boot_setup_fdt(images);
264 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
265 linux_cmdline_legacy(images);
267 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
268 linux_cmdline_append(images);
270 linux_cmdline_dump();
273 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
274 linux_env_legacy(images);
278 static void boot_jump_linux(bootm_headers_t *images)
280 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
281 kernel_entry_t kernel = (kernel_entry_t) images->ep;
282 ulong linux_extra = 0;
284 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
286 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
288 if (CONFIG_IS_ENABLED(MALTA))
289 linux_extra = gd->ram_size;
291 #if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
292 bootstage_fdt_add_report();
294 #if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
298 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
302 kernel(-2, (ulong)images->ft_addr, 0, 0);
304 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
308 int do_bootm_linux(int flag, int argc, char * const argv[],
309 bootm_headers_t *images)
311 /* No need for those on MIPS */
312 if (flag & BOOTM_STATE_OS_BD_T)
316 * Cmdline init has been moved to 'bootm prep' because it has to be
317 * done after relocation of ramdisk to always pass correct values
318 * for rd_start and rd_size to Linux kernel.
320 if (flag & BOOTM_STATE_OS_CMDLINE)
323 if (flag & BOOTM_STATE_OS_PREP) {
324 boot_prep_linux(images);
328 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
329 boot_jump_linux(images);
333 /* does not return */