blob: 6a6764ac2db290aee07e4f9b2b3eb3ab482c818c [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>
wdenkbb1b8262003-03-27 12:09:35 +000012#include <asm/addrspace.h>
Purna Chandra Mandal432549f2016-04-18 18:31:38 +053013#include <asm/io.h>
wdenkbb1b8262003-03-27 12:09:35 +000014
Wolfgang Denk6405a152006-03-31 18:32:53 +020015DECLARE_GLOBAL_DATA_PTR;
16
wdenkbb1b8262003-03-27 12:09:35 +000017#define LINUX_MAX_ENVS 256
18#define LINUX_MAX_ARGS 256
19
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020020static int linux_argc;
21static char **linux_argv;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020022static char *linux_argp;
wdenkbb1b8262003-03-27 12:09:35 +000023
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020024static char **linux_env;
25static char *linux_env_p;
26static int linux_env_idx;
wdenkbb1b8262003-03-27 12:09:35 +000027
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020028static ulong arch_get_sp(void)
29{
30 ulong ret;
31
32 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
33
34 return ret;
35}
36
37void arch_lmb_reserve(struct lmb *lmb)
38{
39 ulong sp;
40
41 sp = arch_get_sp();
42 debug("## Current stack ends at 0x%08lx\n", sp);
43
44 /* adjust sp by 4K to be safe */
45 sp -= 4096;
Paul Burton0ecd9032016-09-26 19:28:56 +010046 lmb_reserve(lmb, sp, gd->ram_top - sp);
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020047}
48
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020049static void linux_cmdline_init(void)
50{
51 linux_argc = 1;
52 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
53 linux_argv[0] = 0;
54 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
55}
56
57static void linux_cmdline_set(const char *value, size_t len)
58{
59 linux_argv[linux_argc] = linux_argp;
60 memcpy(linux_argp, value, len);
61 linux_argp[len] = 0;
62
63 linux_argp += len + 1;
64 linux_argc++;
65}
66
67static void linux_cmdline_dump(void)
68{
69 int i;
70
71 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
72 linux_argv, linux_argp);
73
74 for (i = 1; i < linux_argc; i++)
75 debug(" arg %03d: %s\n", i, linux_argv[i]);
76}
77
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +010078static void linux_cmdline_legacy(bootm_headers_t *images)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020079{
80 const char *bootargs, *next, *quote;
81
82 linux_cmdline_init();
83
Simon Glass64b723f2017-08-03 12:22:12 -060084 bootargs = env_get("bootargs");
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020085 if (!bootargs)
86 return;
87
88 next = bootargs;
89
90 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
91 quote = strchr(bootargs, '"');
92 next = strchr(bootargs, ' ');
93
94 while (next && quote && quote < next) {
95 /*
96 * we found a left quote before the next blank
97 * now we have to find the matching right quote
98 */
99 next = strchr(quote + 1, '"');
100 if (next) {
101 quote = strchr(next + 1, '"');
102 next = strchr(next + 1, ' ');
103 }
104 }
105
106 if (!next)
107 next = bootargs + strlen(bootargs);
108
109 linux_cmdline_set(bootargs, next - bootargs);
110
111 if (*next)
112 next++;
113
114 bootargs = next;
115 }
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +0100116}
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200117
Daniel Schwierzeck2edd5282015-01-14 21:44:13 +0100118static void linux_cmdline_append(bootm_headers_t *images)
119{
120 char buf[24];
121 ulong mem, rd_start, rd_size;
122
123 /* append mem */
124 mem = gd->ram_size >> 20;
125 sprintf(buf, "mem=%luM", mem);
126 linux_cmdline_set(buf, strlen(buf));
127
128 /* append rd_start and rd_size */
129 rd_start = images->initrd_start;
130 rd_size = images->initrd_end - images->initrd_start;
131
132 if (rd_size) {
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));
137 }
138}
139
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200140static void linux_env_init(void)
141{
142 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
143 linux_env[0] = 0;
144 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
145 linux_env_idx = 0;
146}
147
148static void linux_env_set(const char *env_name, const char *env_val)
149{
150 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
151 linux_env[linux_env_idx] = linux_env_p;
152
153 strcpy(linux_env_p, env_name);
154 linux_env_p += strlen(env_name);
155
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100156 if (CONFIG_IS_ENABLED(MALTA)) {
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200157 linux_env_p++;
158 linux_env[++linux_env_idx] = linux_env_p;
159 } else {
160 *linux_env_p++ = '=';
161 }
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200162
163 strcpy(linux_env_p, env_val);
164 linux_env_p += strlen(env_val);
165
166 linux_env_p++;
167 linux_env[++linux_env_idx] = 0;
168 }
169}
170
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100171static void linux_env_legacy(bootm_headers_t *images)
wdenkbb1b8262003-03-27 12:09:35 +0000172{
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200173 char env_buf[12];
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200174 const char *cp;
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200175 ulong rd_start, rd_size;
wdenkbb1b8262003-03-27 12:09:35 +0000176
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100177 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);
181 } else {
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));
185 }
wdenkbb1b8262003-03-27 12:09:35 +0000186
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200187 rd_start = UNCACHED_SDRAM(images->initrd_start);
188 rd_size = images->initrd_end - images->initrd_start;
189
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200190 linux_env_init();
191
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200192 linux_env_set("memsize", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000193
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200194 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200195 linux_env_set("initrd_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000196
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200197 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200198 linux_env_set("initrd_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000199
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200200 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
201 linux_env_set("flash_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000202
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200203 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
204 linux_env_set("flash_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000205
Simon Glass64b723f2017-08-03 12:22:12 -0600206 cp = env_get("ethaddr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200207 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400208 linux_env_set("ethaddr", cp);
Jason McMullancfbe4572008-06-08 23:56:00 -0400209
Simon Glass64b723f2017-08-03 12:22:12 -0600210 cp = env_get("eth1addr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200211 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400212 linux_env_set("eth1addr", cp);
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200213
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100214 if (CONFIG_IS_ENABLED(MALTA)) {
Paul Burton43c4f592013-11-26 17:45:25 +0000215 sprintf(env_buf, "%un8r", gd->baudrate);
216 linux_env_set("modetty0", env_buf);
217 }
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100218}
219
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100220static int boot_reloc_fdt(bootm_headers_t *images)
221{
222 /*
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'.
225 */
226 if (images->state & BOOTM_STATE_FDT) {
227 debug("## FDT already relocated\n");
228 return 0;
229 }
230
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,
234 &images->ft_len);
235#else
236 return 0;
237#endif
238}
239
Alexey Brodkin91a31592018-01-24 20:47:09 +0300240#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
Purna Chandra Mandal432549f2016-04-18 18:31:38 +0530241int arch_fixup_fdt(void *blob)
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100242{
Purna Chandra Mandal432549f2016-04-18 18:31:38 +0530243 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100244 u64 mem_size = gd->ram_size;
245
246 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000247}
Alexey Brodkin91a31592018-01-24 20:47:09 +0300248#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000249
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100250static int boot_setup_fdt(bootm_headers_t *images)
251{
Horatiu Vultur3639e4e2019-04-24 17:21:29 +0200252 images->initrd_start = virt_to_phys((void *)images->initrd_start);
253 images->initrd_end = virt_to_phys((void *)images->initrd_end);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100254 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
255 &images->lmb);
256}
257
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100258static void boot_prep_linux(bootm_headers_t *images)
259{
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100260 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
261 boot_reloc_fdt(images);
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +0100262 boot_setup_fdt(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100263 } else {
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100264 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
265 linux_cmdline_legacy(images);
266
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100267 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100268 linux_cmdline_append(images);
269
270 linux_cmdline_dump();
271 }
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100272
273 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
274 linux_env_legacy(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100275 }
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100276}
277
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000278static void boot_jump_linux(bootm_headers_t *images)
279{
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200280 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
281 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200282 ulong linux_extra = 0;
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000283
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200284 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000285
286 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
287
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100288 if (CONFIG_IS_ENABLED(MALTA))
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200289 linux_extra = gd->ram_size;
290
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100291#if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100292 bootstage_fdt_add_report();
293#endif
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100294#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100295 bootstage_report();
296#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000297
developer5cbbd712020-04-21 09:28:25 +0200298 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
299 trap_restore();
300
Daniel Schwierzeckd1b29d22015-02-22 16:58:30 +0100301 if (images->ft_len)
302 kernel(-2, (ulong)images->ft_addr, 0, 0);
303 else
304 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
305 linux_extra);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000306}
307
308int do_bootm_linux(int flag, int argc, char * const argv[],
309 bootm_headers_t *images)
310{
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000311 /* No need for those on MIPS */
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200312 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000313 return -1;
314
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100315 /*
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.
319 */
320 if (flag & BOOTM_STATE_OS_CMDLINE)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200321 return 0;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200322
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000323 if (flag & BOOTM_STATE_OS_PREP) {
324 boot_prep_linux(images);
325 return 0;
326 }
327
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100328 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000329 boot_jump_linux(images);
330 return 0;
331 }
Jason McMullancfbe4572008-06-08 23:56:00 -0400332
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100333 /* does not return */
Kumar Gala48626aa2008-08-15 08:24:45 -0500334 return 1;
wdenkbb1b8262003-03-27 12:09:35 +0000335}