]> git.ipfire.org Git - thirdparty/u-boot.git/blame - arch/mips/lib/bootm.c
command: Remove the cmd_tbl_t typedef
[thirdparty/u-boot.git] / arch / mips / lib / bootm.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
c021880a
WD
2/*
3 * (C) Copyright 2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
c021880a
WD
5 */
6
7#include <common.h>
52f24238 8#include <bootstage.h>
4bfd1f5d 9#include <env.h>
c021880a 10#include <image.h>
5002d8cc 11#include <fdt_support.h>
4d72caa5 12#include <lmb.h>
c021880a 13#include <asm/addrspace.h>
fdff5b05 14#include <asm/io.h>
c021880a 15
d87080b7
WD
16DECLARE_GLOBAL_DATA_PTR;
17
c021880a
WD
18#define LINUX_MAX_ENVS 256
19#define LINUX_MAX_ARGS 256
20
e51a6b7a
DS
21static int linux_argc;
22static char **linux_argv;
59e8cbdb 23static char *linux_argp;
c021880a 24
e51a6b7a
DS
25static char **linux_env;
26static char *linux_env_p;
27static int linux_env_idx;
c021880a 28
f66cc1e3
DS
29static ulong arch_get_sp(void)
30{
31 ulong ret;
32
33 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
34
35 return ret;
36}
37
38void arch_lmb_reserve(struct lmb *lmb)
39{
40 ulong sp;
41
42 sp = arch_get_sp();
43 debug("## Current stack ends at 0x%08lx\n", sp);
44
45 /* adjust sp by 4K to be safe */
46 sp -= 4096;
7a3e0f74 47 lmb_reserve(lmb, sp, gd->ram_top - sp);
f66cc1e3
DS
48}
49
59e8cbdb
DS
50static void linux_cmdline_init(void)
51{
52 linux_argc = 1;
53 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
54 linux_argv[0] = 0;
55 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
56}
57
58static void linux_cmdline_set(const char *value, size_t len)
59{
60 linux_argv[linux_argc] = linux_argp;
61 memcpy(linux_argp, value, len);
62 linux_argp[len] = 0;
63
64 linux_argp += len + 1;
65 linux_argc++;
66}
67
68static void linux_cmdline_dump(void)
69{
70 int i;
71
72 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
73 linux_argv, linux_argp);
74
75 for (i = 1; i < linux_argc; i++)
76 debug(" arg %03d: %s\n", i, linux_argv[i]);
77}
78
25fc664f 79static void linux_cmdline_legacy(bootm_headers_t *images)
59e8cbdb
DS
80{
81 const char *bootargs, *next, *quote;
82
83 linux_cmdline_init();
84
00caae6d 85 bootargs = env_get("bootargs");
59e8cbdb
DS
86 if (!bootargs)
87 return;
88
89 next = bootargs;
90
91 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
92 quote = strchr(bootargs, '"');
93 next = strchr(bootargs, ' ');
94
95 while (next && quote && quote < next) {
96 /*
97 * we found a left quote before the next blank
98 * now we have to find the matching right quote
99 */
100 next = strchr(quote + 1, '"');
101 if (next) {
102 quote = strchr(next + 1, '"');
103 next = strchr(next + 1, ' ');
104 }
105 }
106
107 if (!next)
108 next = bootargs + strlen(bootargs);
109
110 linux_cmdline_set(bootargs, next - bootargs);
111
112 if (*next)
113 next++;
114
115 bootargs = next;
116 }
25fc664f 117}
59e8cbdb 118
8cec725a
DS
119static void linux_cmdline_append(bootm_headers_t *images)
120{
121 char buf[24];
122 ulong mem, rd_start, rd_size;
123
124 /* append mem */
125 mem = gd->ram_size >> 20;
126 sprintf(buf, "mem=%luM", mem);
127 linux_cmdline_set(buf, strlen(buf));
128
129 /* append rd_start and rd_size */
130 rd_start = images->initrd_start;
131 rd_size = images->initrd_end - images->initrd_start;
132
133 if (rd_size) {
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));
138 }
139}
140
15f8aa90
DS
141static void linux_env_init(void)
142{
143 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
144 linux_env[0] = 0;
145 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
146 linux_env_idx = 0;
147}
148
149static void linux_env_set(const char *env_name, const char *env_val)
150{
151 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
152 linux_env[linux_env_idx] = linux_env_p;
153
154 strcpy(linux_env_p, env_name);
155 linux_env_p += strlen(env_name);
156
347ea94e 157 if (CONFIG_IS_ENABLED(MALTA)) {
b87493f4
DS
158 linux_env_p++;
159 linux_env[++linux_env_idx] = linux_env_p;
160 } else {
161 *linux_env_p++ = '=';
162 }
15f8aa90
DS
163
164 strcpy(linux_env_p, env_val);
165 linux_env_p += strlen(env_val);
166
167 linux_env_p++;
168 linux_env[++linux_env_idx] = 0;
169 }
170}
171
ca65e585 172static void linux_env_legacy(bootm_headers_t *images)
c021880a 173{
e51a6b7a 174 char env_buf[12];
15f8aa90 175 const char *cp;
6c154552 176 ulong rd_start, rd_size;
5da627a4 177
347ea94e
DS
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);
182 } else {
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));
186 }
c021880a 187
6c154552
DS
188 rd_start = UNCACHED_SDRAM(images->initrd_start);
189 rd_size = images->initrd_end - images->initrd_start;
190
15f8aa90
DS
191 linux_env_init();
192
e51a6b7a 193 linux_env_set("memsize", env_buf);
c021880a 194
6c154552 195 sprintf(env_buf, "0x%08lX", rd_start);
e51a6b7a 196 linux_env_set("initrd_start", env_buf);
c021880a 197
6c154552 198 sprintf(env_buf, "0x%lX", rd_size);
e51a6b7a 199 linux_env_set("initrd_size", env_buf);
c021880a 200
e51a6b7a
DS
201 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
202 linux_env_set("flash_start", env_buf);
c021880a 203
e51a6b7a
DS
204 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
205 linux_env_set("flash_size", env_buf);
c021880a 206
00caae6d 207 cp = env_get("ethaddr");
e51a6b7a 208 if (cp)
e7c37452 209 linux_env_set("ethaddr", cp);
e7c37452 210
00caae6d 211 cp = env_get("eth1addr");
e51a6b7a 212 if (cp)
e7c37452 213 linux_env_set("eth1addr", cp);
b87493f4 214
347ea94e 215 if (CONFIG_IS_ENABLED(MALTA)) {
d18d49d7
PB
216 sprintf(env_buf, "%un8r", gd->baudrate);
217 linux_env_set("modetty0", env_buf);
218 }
0ea7213f
GJ
219}
220
2bb5b638
DS
221static int boot_reloc_fdt(bootm_headers_t *images)
222{
223 /*
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'.
226 */
227 if (images->state & BOOTM_STATE_FDT) {
228 debug("## FDT already relocated\n");
229 return 0;
230 }
231
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,
235 &images->ft_len);
236#else
237 return 0;
238#endif
239}
240
4280342a 241#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
fdff5b05 242int arch_fixup_fdt(void *blob)
2bb5b638 243{
fdff5b05 244 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
2bb5b638
DS
245 u64 mem_size = gd->ram_size;
246
247 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
2bb5b638 248}
4280342a 249#endif
2bb5b638
DS
250
251static int boot_setup_fdt(bootm_headers_t *images)
252{
6943cc97
HV
253 images->initrd_start = virt_to_phys((void *)images->initrd_start);
254 images->initrd_end = virt_to_phys((void *)images->initrd_end);
2bb5b638
DS
255 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
256 &images->lmb);
257}
258
ca65e585
DS
259static void boot_prep_linux(bootm_headers_t *images)
260{
2bb5b638
DS
261 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
262 boot_reloc_fdt(images);
5002d8cc 263 boot_setup_fdt(images);
2bb5b638 264 } else {
2bb5b638
DS
265 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
266 linux_cmdline_legacy(images);
267
48bfc31b 268 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
2bb5b638
DS
269 linux_cmdline_append(images);
270
271 linux_cmdline_dump();
272 }
48bfc31b
ZLK
273
274 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
275 linux_env_legacy(images);
2bb5b638 276 }
ca65e585
DS
277}
278
0ea7213f
GJ
279static void boot_jump_linux(bootm_headers_t *images)
280{
c4b37847
DS
281 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
282 kernel_entry_t kernel = (kernel_entry_t) images->ep;
b87493f4 283 ulong linux_extra = 0;
0ea7213f 284
c4b37847 285 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
0ea7213f
GJ
286
287 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
288
347ea94e 289 if (CONFIG_IS_ENABLED(MALTA))
b87493f4
DS
290 linux_extra = gd->ram_size;
291
347ea94e 292#if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
e13a50b3
DS
293 bootstage_fdt_add_report();
294#endif
347ea94e 295#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
e13a50b3
DS
296 bootstage_report();
297#endif
0ea7213f 298
71059736
WG
299 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
300 trap_restore();
301
90b1c9fa
DS
302 if (images->ft_len)
303 kernel(-2, (ulong)images->ft_addr, 0, 0);
304 else
305 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
306 linux_extra);
0ea7213f
GJ
307}
308
09140113
SG
309int do_bootm_linux(int flag, int argc, char *const argv[],
310 bootm_headers_t *images)
0ea7213f 311{
9c170e2e 312 /* No need for those on MIPS */
59e8cbdb 313 if (flag & BOOTM_STATE_OS_BD_T)
9c170e2e
GJ
314 return -1;
315
2bb5b638
DS
316 /*
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.
320 */
321 if (flag & BOOTM_STATE_OS_CMDLINE)
59e8cbdb 322 return 0;
59e8cbdb 323
9c170e2e
GJ
324 if (flag & BOOTM_STATE_OS_PREP) {
325 boot_prep_linux(images);
326 return 0;
327 }
328
2bb5b638 329 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
9c170e2e
GJ
330 boot_jump_linux(images);
331 return 0;
332 }
e7c37452 333
cd7c596e 334 /* does not return */
40d7e99d 335 return 1;
c021880a 336}