1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 #include <fdt_support.h>
13 #include <asm/addrspace.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 #define LINUX_MAX_ENVS 256
19 #define LINUX_MAX_ARGS 256
21 static int linux_argc;
22 static char **linux_argv;
23 static char *linux_argp;
25 static char **linux_env;
26 static char *linux_env_p;
27 static int linux_env_idx;
29 static ulong arch_get_sp(void)
33 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
38 void arch_lmb_reserve(struct lmb *lmb)
43 debug("## Current stack ends at 0x%08lx\n", sp);
45 /* adjust sp by 4K to be safe */
47 lmb_reserve(lmb, sp, gd->ram_top - sp);
50 static void linux_cmdline_init(void)
53 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
55 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
58 static void linux_cmdline_set(const char *value, size_t len)
60 linux_argv[linux_argc] = linux_argp;
61 memcpy(linux_argp, value, len);
64 linux_argp += len + 1;
68 static void linux_cmdline_dump(void)
72 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
73 linux_argv, linux_argp);
75 for (i = 1; i < linux_argc; i++)
76 debug(" arg %03d: %s\n", i, linux_argv[i]);
79 static void linux_cmdline_legacy(bootm_headers_t *images)
81 const char *bootargs, *next, *quote;
85 bootargs = env_get("bootargs");
91 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
92 quote = strchr(bootargs, '"');
93 next = strchr(bootargs, ' ');
95 while (next && quote && quote < next) {
97 * we found a left quote before the next blank
98 * now we have to find the matching right quote
100 next = strchr(quote + 1, '"');
102 quote = strchr(next + 1, '"');
103 next = strchr(next + 1, ' ');
108 next = bootargs + strlen(bootargs);
110 linux_cmdline_set(bootargs, next - bootargs);
119 static void linux_cmdline_append(bootm_headers_t *images)
122 ulong mem, rd_start, rd_size;
125 mem = gd->ram_size >> 20;
126 sprintf(buf, "mem=%luM", mem);
127 linux_cmdline_set(buf, strlen(buf));
129 /* append rd_start and rd_size */
130 rd_start = images->initrd_start;
131 rd_size = images->initrd_end - images->initrd_start;
134 sprintf(buf, "rd_start=0x%08lX", rd_start);
135 linux_cmdline_set(buf, strlen(buf));
136 sprintf(buf, "rd_size=0x%lX", rd_size);
137 linux_cmdline_set(buf, strlen(buf));
141 static void linux_env_init(void)
143 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
145 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
149 static void linux_env_set(const char *env_name, const char *env_val)
151 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
152 linux_env[linux_env_idx] = linux_env_p;
154 strcpy(linux_env_p, env_name);
155 linux_env_p += strlen(env_name);
157 if (CONFIG_IS_ENABLED(MALTA)) {
159 linux_env[++linux_env_idx] = linux_env_p;
161 *linux_env_p++ = '=';
164 strcpy(linux_env_p, env_val);
165 linux_env_p += strlen(env_val);
168 linux_env[++linux_env_idx] = 0;
172 static void linux_env_legacy(bootm_headers_t *images)
176 ulong rd_start, rd_size;
178 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
179 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
180 debug("## Giving linux memsize in bytes, %lu\n",
181 (ulong)gd->ram_size);
183 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
184 debug("## Giving linux memsize in MB, %lu\n",
185 (ulong)(gd->ram_size >> 20));
188 rd_start = UNCACHED_SDRAM(images->initrd_start);
189 rd_size = images->initrd_end - images->initrd_start;
193 linux_env_set("memsize", env_buf);
195 sprintf(env_buf, "0x%08lX", rd_start);
196 linux_env_set("initrd_start", env_buf);
198 sprintf(env_buf, "0x%lX", rd_size);
199 linux_env_set("initrd_size", env_buf);
201 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
202 linux_env_set("flash_start", env_buf);
204 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
205 linux_env_set("flash_size", env_buf);
207 cp = env_get("ethaddr");
209 linux_env_set("ethaddr", cp);
211 cp = env_get("eth1addr");
213 linux_env_set("eth1addr", cp);
215 if (CONFIG_IS_ENABLED(MALTA)) {
216 sprintf(env_buf, "%un8r", gd->baudrate);
217 linux_env_set("modetty0", env_buf);
221 static int boot_reloc_fdt(bootm_headers_t *images)
224 * In case of legacy uImage's, relocation of FDT is already done
225 * by do_bootm_states() and should not repeated in 'bootm prep'.
227 if (images->state & BOOTM_STATE_FDT) {
228 debug("## FDT already relocated\n");
232 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
233 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
234 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
241 #if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
242 int arch_fixup_fdt(void *blob)
244 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
245 u64 mem_size = gd->ram_size;
247 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
251 static int boot_setup_fdt(bootm_headers_t *images)
253 images->initrd_start = virt_to_phys((void *)images->initrd_start);
254 images->initrd_end = virt_to_phys((void *)images->initrd_end);
255 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
259 static void boot_prep_linux(bootm_headers_t *images)
261 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
262 boot_reloc_fdt(images);
263 boot_setup_fdt(images);
265 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
266 linux_cmdline_legacy(images);
268 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
269 linux_cmdline_append(images);
271 linux_cmdline_dump();
274 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
275 linux_env_legacy(images);
279 static void boot_jump_linux(bootm_headers_t *images)
281 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
282 kernel_entry_t kernel = (kernel_entry_t) images->ep;
283 ulong linux_extra = 0;
285 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
287 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
289 if (CONFIG_IS_ENABLED(MALTA))
290 linux_extra = gd->ram_size;
292 #if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
293 bootstage_fdt_add_report();
295 #if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
299 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
303 kernel(-2, (ulong)images->ft_addr, 0, 0);
305 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
309 int do_bootm_linux(int flag, int argc, char * const argv[],
310 bootm_headers_t *images)
312 /* No need for those on MIPS */
313 if (flag & BOOTM_STATE_OS_BD_T)
317 * Cmdline init has been moved to 'bootm prep' because it has to be
318 * done after relocation of ramdisk to always pass correct values
319 * for rd_start and rd_size to Linux kernel.
321 if (flag & BOOTM_STATE_OS_CMDLINE)
324 if (flag & BOOTM_STATE_OS_PREP) {
325 boot_prep_linux(images);
329 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
330 boot_jump_linux(images);
334 /* does not return */