blob: ce2b076b7d4581f8597dbaa01976a2ca31443b9d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkbb1b8262003-03-27 12:09:35 +00002/*
3 * (C) Copyright 2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkbb1b8262003-03-27 12:09:35 +00005 */
6
7#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -06008#include <bootstage.h>
Simon Glass79fd2142019-08-01 09:46:43 -06009#include <env.h>
wdenkbb1b8262003-03-27 12:09:35 +000010#include <image.h>
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +010011#include <fdt_support.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060012#include <lmb.h>
wdenkbb1b8262003-03-27 12:09:35 +000013#include <asm/addrspace.h>
Purna Chandra Mandal432549f2016-04-18 18:31:38 +053014#include <asm/io.h>
wdenkbb1b8262003-03-27 12:09:35 +000015
Wolfgang Denk6405a152006-03-31 18:32:53 +020016DECLARE_GLOBAL_DATA_PTR;
17
wdenkbb1b8262003-03-27 12:09:35 +000018#define LINUX_MAX_ENVS 256
19#define LINUX_MAX_ARGS 256
20
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020021static int linux_argc;
22static char **linux_argv;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020023static char *linux_argp;
wdenkbb1b8262003-03-27 12:09:35 +000024
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020025static char **linux_env;
26static char *linux_env_p;
27static int linux_env_idx;
wdenkbb1b8262003-03-27 12:09:35 +000028
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020029static 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;
Paul Burton0ecd9032016-09-26 19:28:56 +010047 lmb_reserve(lmb, sp, gd->ram_top - sp);
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020048}
49
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020050static 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
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +010079static void linux_cmdline_legacy(bootm_headers_t *images)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020080{
81 const char *bootargs, *next, *quote;
82
83 linux_cmdline_init();
84
Simon Glass64b723f2017-08-03 12:22:12 -060085 bootargs = env_get("bootargs");
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020086 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 }
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +0100117}
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200118
Daniel Schwierzeck2edd5282015-01-14 21:44:13 +0100119static 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
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200141static 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
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100157 if (CONFIG_IS_ENABLED(MALTA)) {
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200158 linux_env_p++;
159 linux_env[++linux_env_idx] = linux_env_p;
160 } else {
161 *linux_env_p++ = '=';
162 }
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200163
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
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100172static void linux_env_legacy(bootm_headers_t *images)
wdenkbb1b8262003-03-27 12:09:35 +0000173{
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200174 char env_buf[12];
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200175 const char *cp;
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200176 ulong rd_start, rd_size;
wdenkbb1b8262003-03-27 12:09:35 +0000177
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100178 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 }
wdenkbb1b8262003-03-27 12:09:35 +0000187
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200188 rd_start = UNCACHED_SDRAM(images->initrd_start);
189 rd_size = images->initrd_end - images->initrd_start;
190
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200191 linux_env_init();
192
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200193 linux_env_set("memsize", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000194
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200195 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200196 linux_env_set("initrd_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000197
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200198 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200199 linux_env_set("initrd_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000200
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200201 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
202 linux_env_set("flash_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000203
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200204 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
205 linux_env_set("flash_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000206
Simon Glass64b723f2017-08-03 12:22:12 -0600207 cp = env_get("ethaddr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200208 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400209 linux_env_set("ethaddr", cp);
Jason McMullancfbe4572008-06-08 23:56:00 -0400210
Simon Glass64b723f2017-08-03 12:22:12 -0600211 cp = env_get("eth1addr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200212 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400213 linux_env_set("eth1addr", cp);
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200214
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100215 if (CONFIG_IS_ENABLED(MALTA)) {
Paul Burton43c4f592013-11-26 17:45:25 +0000216 sprintf(env_buf, "%un8r", gd->baudrate);
217 linux_env_set("modetty0", env_buf);
218 }
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100219}
220
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100221static 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
Alexey Brodkin91a31592018-01-24 20:47:09 +0300241#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
Purna Chandra Mandal432549f2016-04-18 18:31:38 +0530242int arch_fixup_fdt(void *blob)
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100243{
Purna Chandra Mandal432549f2016-04-18 18:31:38 +0530244 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100245 u64 mem_size = gd->ram_size;
246
247 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000248}
Alexey Brodkin91a31592018-01-24 20:47:09 +0300249#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000250
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100251static int boot_setup_fdt(bootm_headers_t *images)
252{
Horatiu Vultur3639e4e2019-04-24 17:21:29 +0200253 images->initrd_start = virt_to_phys((void *)images->initrd_start);
254 images->initrd_end = virt_to_phys((void *)images->initrd_end);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100255 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
256 &images->lmb);
257}
258
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100259static void boot_prep_linux(bootm_headers_t *images)
260{
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100261 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
262 boot_reloc_fdt(images);
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +0100263 boot_setup_fdt(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100264 } else {
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100265 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
266 linux_cmdline_legacy(images);
267
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100268 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100269 linux_cmdline_append(images);
270
271 linux_cmdline_dump();
272 }
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100273
274 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
275 linux_env_legacy(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100276 }
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100277}
278
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000279static void boot_jump_linux(bootm_headers_t *images)
280{
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200281 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
282 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200283 ulong linux_extra = 0;
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000284
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200285 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000286
287 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
288
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100289 if (CONFIG_IS_ENABLED(MALTA))
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200290 linux_extra = gd->ram_size;
291
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100292#if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100293 bootstage_fdt_add_report();
294#endif
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100295#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100296 bootstage_report();
297#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000298
developer5cbbd712020-04-21 09:28:25 +0200299 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
300 trap_restore();
301
Daniel Schwierzeckd1b29d22015-02-22 16:58:30 +0100302 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);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000307}
308
309int do_bootm_linux(int flag, int argc, char * const argv[],
310 bootm_headers_t *images)
311{
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000312 /* No need for those on MIPS */
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200313 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000314 return -1;
315
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100316 /*
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)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200322 return 0;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200323
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000324 if (flag & BOOTM_STATE_OS_PREP) {
325 boot_prep_linux(images);
326 return 0;
327 }
328
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100329 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000330 boot_jump_linux(images);
331 return 0;
332 }
Jason McMullancfbe4572008-06-08 23:56:00 -0400333
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100334 /* does not return */
Kumar Gala48626aa2008-08-15 08:24:45 -0500335 return 1;
wdenkbb1b8262003-03-27 12:09:35 +0000336}