blob: d6d2f7d9d031451cdfec61de4b73d8e61e31202a [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
Simon Glass1ea97892020-05-10 11:40:00 -06007#include <bootstage.h>
Simon Glass79fd2142019-08-01 09:46:43 -06008#include <env.h>
wdenkbb1b8262003-03-27 12:09:35 +00009#include <image.h>
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +010010#include <fdt_support.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060011#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
wdenkbb1b8262003-03-27 12:09:35 +000013#include <asm/addrspace.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Purna Chandra Mandal432549f2016-04-18 18:31:38 +053015#include <asm/io.h>
wdenkbb1b8262003-03-27 12:09:35 +000016
Wolfgang Denk6405a152006-03-31 18:32:53 +020017DECLARE_GLOBAL_DATA_PTR;
18
wdenkbb1b8262003-03-27 12:09:35 +000019#define LINUX_MAX_ENVS 256
20#define LINUX_MAX_ARGS 256
21
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020022static int linux_argc;
23static char **linux_argv;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020024static char *linux_argp;
wdenkbb1b8262003-03-27 12:09:35 +000025
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020026static char **linux_env;
27static char *linux_env_p;
28static int linux_env_idx;
wdenkbb1b8262003-03-27 12:09:35 +000029
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020030static ulong arch_get_sp(void)
31{
32 ulong ret;
33
34 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
35
36 return ret;
37}
38
39void arch_lmb_reserve(struct lmb *lmb)
40{
Marek Vasutd0dd68f2021-09-10 22:47:10 +020041 arch_lmb_reserve_generic(lmb, arch_get_sp(), gd->ram_top, 4096);
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020042}
43
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020044static void linux_cmdline_init(void)
45{
46 linux_argc = 1;
Daniel Schwierzecke61db372020-07-12 01:46:15 +020047 linux_argv = (char **)CKSEG1ADDR(gd->bd->bi_boot_params);
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020048 linux_argv[0] = 0;
49 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
50}
51
52static void linux_cmdline_set(const char *value, size_t len)
53{
54 linux_argv[linux_argc] = linux_argp;
55 memcpy(linux_argp, value, len);
56 linux_argp[len] = 0;
57
58 linux_argp += len + 1;
59 linux_argc++;
60}
61
62static void linux_cmdline_dump(void)
63{
64 int i;
65
66 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
67 linux_argv, linux_argp);
68
69 for (i = 1; i < linux_argc; i++)
70 debug(" arg %03d: %s\n", i, linux_argv[i]);
71}
72
Simon Glassdf00afa2022-09-06 20:26:50 -060073static void linux_cmdline_legacy(struct bootm_headers *images)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020074{
75 const char *bootargs, *next, *quote;
76
77 linux_cmdline_init();
78
Simon Glass64b723f2017-08-03 12:22:12 -060079 bootargs = env_get("bootargs");
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020080 if (!bootargs)
81 return;
82
83 next = bootargs;
84
85 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
86 quote = strchr(bootargs, '"');
87 next = strchr(bootargs, ' ');
88
89 while (next && quote && quote < next) {
90 /*
91 * we found a left quote before the next blank
92 * now we have to find the matching right quote
93 */
94 next = strchr(quote + 1, '"');
95 if (next) {
96 quote = strchr(next + 1, '"');
97 next = strchr(next + 1, ' ');
98 }
99 }
100
101 if (!next)
102 next = bootargs + strlen(bootargs);
103
104 linux_cmdline_set(bootargs, next - bootargs);
105
106 if (*next)
107 next++;
108
109 bootargs = next;
110 }
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +0100111}
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200112
Simon Glassdf00afa2022-09-06 20:26:50 -0600113static void linux_cmdline_append(struct bootm_headers *images)
Daniel Schwierzeck2edd5282015-01-14 21:44:13 +0100114{
115 char buf[24];
116 ulong mem, rd_start, rd_size;
117
118 /* append mem */
119 mem = gd->ram_size >> 20;
120 sprintf(buf, "mem=%luM", mem);
121 linux_cmdline_set(buf, strlen(buf));
122
123 /* append rd_start and rd_size */
124 rd_start = images->initrd_start;
125 rd_size = images->initrd_end - images->initrd_start;
126
127 if (rd_size) {
128 sprintf(buf, "rd_start=0x%08lX", rd_start);
129 linux_cmdline_set(buf, strlen(buf));
130 sprintf(buf, "rd_size=0x%lX", rd_size);
131 linux_cmdline_set(buf, strlen(buf));
132 }
133}
134
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200135static void linux_env_init(void)
136{
137 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
138 linux_env[0] = 0;
139 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
140 linux_env_idx = 0;
141}
142
143static void linux_env_set(const char *env_name, const char *env_val)
144{
145 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
146 linux_env[linux_env_idx] = linux_env_p;
147
148 strcpy(linux_env_p, env_name);
149 linux_env_p += strlen(env_name);
150
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100151 if (CONFIG_IS_ENABLED(MALTA)) {
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200152 linux_env_p++;
153 linux_env[++linux_env_idx] = linux_env_p;
154 } else {
155 *linux_env_p++ = '=';
156 }
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200157
158 strcpy(linux_env_p, env_val);
159 linux_env_p += strlen(env_val);
160
161 linux_env_p++;
162 linux_env[++linux_env_idx] = 0;
163 }
164}
165
Simon Glassdf00afa2022-09-06 20:26:50 -0600166static void linux_env_legacy(struct bootm_headers *images)
wdenkbb1b8262003-03-27 12:09:35 +0000167{
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200168 char env_buf[12];
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200169 const char *cp;
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200170 ulong rd_start, rd_size;
wdenkbb1b8262003-03-27 12:09:35 +0000171
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100172 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
173 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
174 debug("## Giving linux memsize in bytes, %lu\n",
175 (ulong)gd->ram_size);
176 } else {
177 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
178 debug("## Giving linux memsize in MB, %lu\n",
179 (ulong)(gd->ram_size >> 20));
180 }
wdenkbb1b8262003-03-27 12:09:35 +0000181
Daniel Schwierzecke61db372020-07-12 01:46:15 +0200182 rd_start = CKSEG1ADDR(images->initrd_start);
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200183 rd_size = images->initrd_end - images->initrd_start;
184
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200185 linux_env_init();
186
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200187 linux_env_set("memsize", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000188
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200189 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200190 linux_env_set("initrd_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000191
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200192 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200193 linux_env_set("initrd_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000194
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200195 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
196 linux_env_set("flash_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000197
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200198 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
199 linux_env_set("flash_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000200
Simon Glass64b723f2017-08-03 12:22:12 -0600201 cp = env_get("ethaddr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200202 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400203 linux_env_set("ethaddr", cp);
Jason McMullancfbe4572008-06-08 23:56:00 -0400204
Simon Glass64b723f2017-08-03 12:22:12 -0600205 cp = env_get("eth1addr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200206 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400207 linux_env_set("eth1addr", cp);
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200208
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100209 if (CONFIG_IS_ENABLED(MALTA)) {
Paul Burton43c4f592013-11-26 17:45:25 +0000210 sprintf(env_buf, "%un8r", gd->baudrate);
211 linux_env_set("modetty0", env_buf);
212 }
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100213}
214
Simon Glassdf00afa2022-09-06 20:26:50 -0600215static int boot_reloc_fdt(struct bootm_headers *images)
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100216{
217 /*
218 * In case of legacy uImage's, relocation of FDT is already done
219 * by do_bootm_states() and should not repeated in 'bootm prep'.
220 */
221 if (images->state & BOOTM_STATE_FDT) {
222 debug("## FDT already relocated\n");
223 return 0;
224 }
225
226#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
227 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
228 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
229 &images->ft_len);
230#else
231 return 0;
232#endif
233}
234
Alexey Brodkin91a31592018-01-24 20:47:09 +0300235#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
Purna Chandra Mandal432549f2016-04-18 18:31:38 +0530236int arch_fixup_fdt(void *blob)
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100237{
Stefan Roesea13a2aa2020-08-12 13:16:36 +0200238 u64 mem_start = virt_to_phys((void *)gd->ram_base);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100239 u64 mem_size = gd->ram_size;
240
241 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000242}
Alexey Brodkin91a31592018-01-24 20:47:09 +0300243#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000244
Simon Glassdf00afa2022-09-06 20:26:50 -0600245static int boot_setup_fdt(struct bootm_headers *images)
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100246{
Horatiu Vultur3639e4e2019-04-24 17:21:29 +0200247 images->initrd_start = virt_to_phys((void *)images->initrd_start);
248 images->initrd_end = virt_to_phys((void *)images->initrd_end);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100249 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
250 &images->lmb);
251}
252
Simon Glassdf00afa2022-09-06 20:26:50 -0600253static void boot_prep_linux(struct bootm_headers *images)
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100254{
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100255 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
256 boot_reloc_fdt(images);
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +0100257 boot_setup_fdt(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100258 } else {
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100259 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
260 linux_cmdline_legacy(images);
261
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100262 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100263 linux_cmdline_append(images);
264
265 linux_cmdline_dump();
266 }
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100267
268 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
269 linux_env_legacy(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100270 }
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100271}
272
Simon Glassdf00afa2022-09-06 20:26:50 -0600273static void boot_jump_linux(struct bootm_headers *images)
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000274{
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200275 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
276 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200277 ulong linux_extra = 0;
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000278
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200279 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000280
281 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
282
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100283 if (CONFIG_IS_ENABLED(MALTA))
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200284 linux_extra = gd->ram_size;
285
Simon Glassf499b4d2023-02-05 15:36:19 -0700286#if IS_ENABLED(CONFIG_BOOTSTAGE_FDT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100287 bootstage_fdt_add_report();
288#endif
Simon Glasscd197952023-02-05 15:36:20 -0700289#if IS_ENABLED(CONFIG_BOOTSTAGE_REPORT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100290 bootstage_report();
291#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000292
developer5cbbd712020-04-21 09:28:25 +0200293 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
294 trap_restore();
295
Daniel Schwierzeckd1b29d22015-02-22 16:58:30 +0100296 if (images->ft_len)
297 kernel(-2, (ulong)images->ft_addr, 0, 0);
298 else
299 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
300 linux_extra);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000301}
302
Simon Glassed38aef2020-05-10 11:40:03 -0600303int do_bootm_linux(int flag, int argc, char *const argv[],
Simon Glassdf00afa2022-09-06 20:26:50 -0600304 struct bootm_headers *images)
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000305{
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000306 /* No need for those on MIPS */
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200307 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000308 return -1;
309
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100310 /*
311 * Cmdline init has been moved to 'bootm prep' because it has to be
312 * done after relocation of ramdisk to always pass correct values
313 * for rd_start and rd_size to Linux kernel.
314 */
315 if (flag & BOOTM_STATE_OS_CMDLINE)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200316 return 0;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200317
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000318 if (flag & BOOTM_STATE_OS_PREP) {
319 boot_prep_linux(images);
320 return 0;
321 }
322
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100323 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000324 boot_jump_linux(images);
325 return 0;
326 }
Jason McMullancfbe4572008-06-08 23:56:00 -0400327
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100328 /* does not return */
Kumar Gala48626aa2008-08-15 08:24:45 -0500329 return 1;
wdenkbb1b8262003-03-27 12:09:35 +0000330}