blob: fde90fced44f88fc145c0f561726be81e0a2b6ca [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>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
wdenkbb1b8262003-03-27 12:09:35 +000014#include <asm/addrspace.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Purna Chandra Mandal432549f2016-04-18 18:31:38 +053016#include <asm/io.h>
wdenkbb1b8262003-03-27 12:09:35 +000017
Wolfgang Denk6405a152006-03-31 18:32:53 +020018DECLARE_GLOBAL_DATA_PTR;
19
wdenkbb1b8262003-03-27 12:09:35 +000020#define LINUX_MAX_ENVS 256
21#define LINUX_MAX_ARGS 256
22
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020023static int linux_argc;
24static char **linux_argv;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020025static char *linux_argp;
wdenkbb1b8262003-03-27 12:09:35 +000026
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020027static char **linux_env;
28static char *linux_env_p;
29static int linux_env_idx;
wdenkbb1b8262003-03-27 12:09:35 +000030
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020031static ulong arch_get_sp(void)
32{
33 ulong ret;
34
35 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
36
37 return ret;
38}
39
40void arch_lmb_reserve(struct lmb *lmb)
41{
42 ulong sp;
43
44 sp = arch_get_sp();
45 debug("## Current stack ends at 0x%08lx\n", sp);
46
47 /* adjust sp by 4K to be safe */
48 sp -= 4096;
Paul Burton0ecd9032016-09-26 19:28:56 +010049 lmb_reserve(lmb, sp, gd->ram_top - sp);
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020050}
51
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020052static void linux_cmdline_init(void)
53{
54 linux_argc = 1;
Daniel Schwierzecke61db372020-07-12 01:46:15 +020055 linux_argv = (char **)CKSEG1ADDR(gd->bd->bi_boot_params);
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020056 linux_argv[0] = 0;
57 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
58}
59
60static void linux_cmdline_set(const char *value, size_t len)
61{
62 linux_argv[linux_argc] = linux_argp;
63 memcpy(linux_argp, value, len);
64 linux_argp[len] = 0;
65
66 linux_argp += len + 1;
67 linux_argc++;
68}
69
70static void linux_cmdline_dump(void)
71{
72 int i;
73
74 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
75 linux_argv, linux_argp);
76
77 for (i = 1; i < linux_argc; i++)
78 debug(" arg %03d: %s\n", i, linux_argv[i]);
79}
80
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +010081static void linux_cmdline_legacy(bootm_headers_t *images)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020082{
83 const char *bootargs, *next, *quote;
84
85 linux_cmdline_init();
86
Simon Glass64b723f2017-08-03 12:22:12 -060087 bootargs = env_get("bootargs");
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020088 if (!bootargs)
89 return;
90
91 next = bootargs;
92
93 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
94 quote = strchr(bootargs, '"');
95 next = strchr(bootargs, ' ');
96
97 while (next && quote && quote < next) {
98 /*
99 * we found a left quote before the next blank
100 * now we have to find the matching right quote
101 */
102 next = strchr(quote + 1, '"');
103 if (next) {
104 quote = strchr(next + 1, '"');
105 next = strchr(next + 1, ' ');
106 }
107 }
108
109 if (!next)
110 next = bootargs + strlen(bootargs);
111
112 linux_cmdline_set(bootargs, next - bootargs);
113
114 if (*next)
115 next++;
116
117 bootargs = next;
118 }
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +0100119}
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200120
Daniel Schwierzeck2edd5282015-01-14 21:44:13 +0100121static void linux_cmdline_append(bootm_headers_t *images)
122{
123 char buf[24];
124 ulong mem, rd_start, rd_size;
125
126 /* append mem */
127 mem = gd->ram_size >> 20;
128 sprintf(buf, "mem=%luM", mem);
129 linux_cmdline_set(buf, strlen(buf));
130
131 /* append rd_start and rd_size */
132 rd_start = images->initrd_start;
133 rd_size = images->initrd_end - images->initrd_start;
134
135 if (rd_size) {
136 sprintf(buf, "rd_start=0x%08lX", rd_start);
137 linux_cmdline_set(buf, strlen(buf));
138 sprintf(buf, "rd_size=0x%lX", rd_size);
139 linux_cmdline_set(buf, strlen(buf));
140 }
141}
142
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200143static void linux_env_init(void)
144{
145 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
146 linux_env[0] = 0;
147 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
148 linux_env_idx = 0;
149}
150
151static void linux_env_set(const char *env_name, const char *env_val)
152{
153 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
154 linux_env[linux_env_idx] = linux_env_p;
155
156 strcpy(linux_env_p, env_name);
157 linux_env_p += strlen(env_name);
158
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100159 if (CONFIG_IS_ENABLED(MALTA)) {
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200160 linux_env_p++;
161 linux_env[++linux_env_idx] = linux_env_p;
162 } else {
163 *linux_env_p++ = '=';
164 }
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200165
166 strcpy(linux_env_p, env_val);
167 linux_env_p += strlen(env_val);
168
169 linux_env_p++;
170 linux_env[++linux_env_idx] = 0;
171 }
172}
173
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100174static void linux_env_legacy(bootm_headers_t *images)
wdenkbb1b8262003-03-27 12:09:35 +0000175{
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200176 char env_buf[12];
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200177 const char *cp;
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200178 ulong rd_start, rd_size;
wdenkbb1b8262003-03-27 12:09:35 +0000179
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100180 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
181 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
182 debug("## Giving linux memsize in bytes, %lu\n",
183 (ulong)gd->ram_size);
184 } else {
185 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
186 debug("## Giving linux memsize in MB, %lu\n",
187 (ulong)(gd->ram_size >> 20));
188 }
wdenkbb1b8262003-03-27 12:09:35 +0000189
Daniel Schwierzecke61db372020-07-12 01:46:15 +0200190 rd_start = CKSEG1ADDR(images->initrd_start);
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200191 rd_size = images->initrd_end - images->initrd_start;
192
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200193 linux_env_init();
194
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200195 linux_env_set("memsize", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000196
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200197 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200198 linux_env_set("initrd_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000199
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200200 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200201 linux_env_set("initrd_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000202
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200203 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
204 linux_env_set("flash_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000205
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200206 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
207 linux_env_set("flash_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000208
Simon Glass64b723f2017-08-03 12:22:12 -0600209 cp = env_get("ethaddr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200210 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400211 linux_env_set("ethaddr", cp);
Jason McMullancfbe4572008-06-08 23:56:00 -0400212
Simon Glass64b723f2017-08-03 12:22:12 -0600213 cp = env_get("eth1addr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200214 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400215 linux_env_set("eth1addr", cp);
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200216
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100217 if (CONFIG_IS_ENABLED(MALTA)) {
Paul Burton43c4f592013-11-26 17:45:25 +0000218 sprintf(env_buf, "%un8r", gd->baudrate);
219 linux_env_set("modetty0", env_buf);
220 }
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100221}
222
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100223static int boot_reloc_fdt(bootm_headers_t *images)
224{
225 /*
226 * In case of legacy uImage's, relocation of FDT is already done
227 * by do_bootm_states() and should not repeated in 'bootm prep'.
228 */
229 if (images->state & BOOTM_STATE_FDT) {
230 debug("## FDT already relocated\n");
231 return 0;
232 }
233
234#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
235 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
236 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
237 &images->ft_len);
238#else
239 return 0;
240#endif
241}
242
Alexey Brodkin91a31592018-01-24 20:47:09 +0300243#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
Purna Chandra Mandal432549f2016-04-18 18:31:38 +0530244int arch_fixup_fdt(void *blob)
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100245{
Stefan Roesea13a2aa2020-08-12 13:16:36 +0200246 u64 mem_start = virt_to_phys((void *)gd->ram_base);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100247 u64 mem_size = gd->ram_size;
248
249 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000250}
Alexey Brodkin91a31592018-01-24 20:47:09 +0300251#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000252
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100253static int boot_setup_fdt(bootm_headers_t *images)
254{
Horatiu Vultur3639e4e2019-04-24 17:21:29 +0200255 images->initrd_start = virt_to_phys((void *)images->initrd_start);
256 images->initrd_end = virt_to_phys((void *)images->initrd_end);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100257 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
258 &images->lmb);
259}
260
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100261static void boot_prep_linux(bootm_headers_t *images)
262{
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100263 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
264 boot_reloc_fdt(images);
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +0100265 boot_setup_fdt(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100266 } else {
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100267 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
268 linux_cmdline_legacy(images);
269
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100270 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100271 linux_cmdline_append(images);
272
273 linux_cmdline_dump();
274 }
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100275
276 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
277 linux_env_legacy(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100278 }
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100279}
280
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000281static void boot_jump_linux(bootm_headers_t *images)
282{
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200283 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
284 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200285 ulong linux_extra = 0;
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000286
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200287 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000288
289 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
290
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100291 if (CONFIG_IS_ENABLED(MALTA))
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200292 linux_extra = gd->ram_size;
293
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100294#if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100295 bootstage_fdt_add_report();
296#endif
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100297#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100298 bootstage_report();
299#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000300
developer5cbbd712020-04-21 09:28:25 +0200301 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
302 trap_restore();
303
Daniel Schwierzeckd1b29d22015-02-22 16:58:30 +0100304 if (images->ft_len)
305 kernel(-2, (ulong)images->ft_addr, 0, 0);
306 else
307 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
308 linux_extra);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000309}
310
Simon Glassed38aef2020-05-10 11:40:03 -0600311int do_bootm_linux(int flag, int argc, char *const argv[],
312 bootm_headers_t *images)
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000313{
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000314 /* No need for those on MIPS */
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200315 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000316 return -1;
317
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100318 /*
319 * Cmdline init has been moved to 'bootm prep' because it has to be
320 * done after relocation of ramdisk to always pass correct values
321 * for rd_start and rd_size to Linux kernel.
322 */
323 if (flag & BOOTM_STATE_OS_CMDLINE)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200324 return 0;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200325
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000326 if (flag & BOOTM_STATE_OS_PREP) {
327 boot_prep_linux(images);
328 return 0;
329 }
330
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100331 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000332 boot_jump_linux(images);
333 return 0;
334 }
Jason McMullancfbe4572008-06-08 23:56:00 -0400335
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100336 /* does not return */
Kumar Gala48626aa2008-08-15 08:24:45 -0500337 return 1;
wdenkbb1b8262003-03-27 12:09:35 +0000338}