blob: deca5189e33f93c285c7cc68596e9a4c916187c3 [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>
wdenkbb1b8262003-03-27 12:09:35 +00008#include <image.h>
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +01009#include <fdt_support.h>
wdenkbb1b8262003-03-27 12:09:35 +000010#include <asm/addrspace.h>
Purna Chandra Mandal432549f2016-04-18 18:31:38 +053011#include <asm/io.h>
wdenkbb1b8262003-03-27 12:09:35 +000012
Wolfgang Denk6405a152006-03-31 18:32:53 +020013DECLARE_GLOBAL_DATA_PTR;
14
wdenkbb1b8262003-03-27 12:09:35 +000015#define LINUX_MAX_ENVS 256
16#define LINUX_MAX_ARGS 256
17
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020018static int linux_argc;
19static char **linux_argv;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020020static char *linux_argp;
wdenkbb1b8262003-03-27 12:09:35 +000021
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020022static char **linux_env;
23static char *linux_env_p;
24static int linux_env_idx;
wdenkbb1b8262003-03-27 12:09:35 +000025
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020026static ulong arch_get_sp(void)
27{
28 ulong ret;
29
30 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
31
32 return ret;
33}
34
35void arch_lmb_reserve(struct lmb *lmb)
36{
37 ulong sp;
38
39 sp = arch_get_sp();
40 debug("## Current stack ends at 0x%08lx\n", sp);
41
42 /* adjust sp by 4K to be safe */
43 sp -= 4096;
Paul Burton0ecd9032016-09-26 19:28:56 +010044 lmb_reserve(lmb, sp, gd->ram_top - sp);
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020045}
46
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020047static void linux_cmdline_init(void)
48{
49 linux_argc = 1;
50 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
51 linux_argv[0] = 0;
52 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
53}
54
55static void linux_cmdline_set(const char *value, size_t len)
56{
57 linux_argv[linux_argc] = linux_argp;
58 memcpy(linux_argp, value, len);
59 linux_argp[len] = 0;
60
61 linux_argp += len + 1;
62 linux_argc++;
63}
64
65static void linux_cmdline_dump(void)
66{
67 int i;
68
69 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
70 linux_argv, linux_argp);
71
72 for (i = 1; i < linux_argc; i++)
73 debug(" arg %03d: %s\n", i, linux_argv[i]);
74}
75
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +010076static void linux_cmdline_legacy(bootm_headers_t *images)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020077{
78 const char *bootargs, *next, *quote;
79
80 linux_cmdline_init();
81
Simon Glass64b723f2017-08-03 12:22:12 -060082 bootargs = env_get("bootargs");
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020083 if (!bootargs)
84 return;
85
86 next = bootargs;
87
88 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
89 quote = strchr(bootargs, '"');
90 next = strchr(bootargs, ' ');
91
92 while (next && quote && quote < next) {
93 /*
94 * we found a left quote before the next blank
95 * now we have to find the matching right quote
96 */
97 next = strchr(quote + 1, '"');
98 if (next) {
99 quote = strchr(next + 1, '"');
100 next = strchr(next + 1, ' ');
101 }
102 }
103
104 if (!next)
105 next = bootargs + strlen(bootargs);
106
107 linux_cmdline_set(bootargs, next - bootargs);
108
109 if (*next)
110 next++;
111
112 bootargs = next;
113 }
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +0100114}
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200115
Daniel Schwierzeck2edd5282015-01-14 21:44:13 +0100116static void linux_cmdline_append(bootm_headers_t *images)
117{
118 char buf[24];
119 ulong mem, rd_start, rd_size;
120
121 /* append mem */
122 mem = gd->ram_size >> 20;
123 sprintf(buf, "mem=%luM", mem);
124 linux_cmdline_set(buf, strlen(buf));
125
126 /* append rd_start and rd_size */
127 rd_start = images->initrd_start;
128 rd_size = images->initrd_end - images->initrd_start;
129
130 if (rd_size) {
131 sprintf(buf, "rd_start=0x%08lX", rd_start);
132 linux_cmdline_set(buf, strlen(buf));
133 sprintf(buf, "rd_size=0x%lX", rd_size);
134 linux_cmdline_set(buf, strlen(buf));
135 }
136}
137
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200138static void linux_env_init(void)
139{
140 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
141 linux_env[0] = 0;
142 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
143 linux_env_idx = 0;
144}
145
146static void linux_env_set(const char *env_name, const char *env_val)
147{
148 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
149 linux_env[linux_env_idx] = linux_env_p;
150
151 strcpy(linux_env_p, env_name);
152 linux_env_p += strlen(env_name);
153
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100154 if (CONFIG_IS_ENABLED(MALTA)) {
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200155 linux_env_p++;
156 linux_env[++linux_env_idx] = linux_env_p;
157 } else {
158 *linux_env_p++ = '=';
159 }
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200160
161 strcpy(linux_env_p, env_val);
162 linux_env_p += strlen(env_val);
163
164 linux_env_p++;
165 linux_env[++linux_env_idx] = 0;
166 }
167}
168
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100169static void linux_env_legacy(bootm_headers_t *images)
wdenkbb1b8262003-03-27 12:09:35 +0000170{
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200171 char env_buf[12];
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200172 const char *cp;
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200173 ulong rd_start, rd_size;
wdenkbb1b8262003-03-27 12:09:35 +0000174
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100175 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
176 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
177 debug("## Giving linux memsize in bytes, %lu\n",
178 (ulong)gd->ram_size);
179 } else {
180 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
181 debug("## Giving linux memsize in MB, %lu\n",
182 (ulong)(gd->ram_size >> 20));
183 }
wdenkbb1b8262003-03-27 12:09:35 +0000184
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200185 rd_start = UNCACHED_SDRAM(images->initrd_start);
186 rd_size = images->initrd_end - images->initrd_start;
187
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200188 linux_env_init();
189
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200190 linux_env_set("memsize", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000191
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200192 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200193 linux_env_set("initrd_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000194
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200195 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200196 linux_env_set("initrd_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000197
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200198 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
199 linux_env_set("flash_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000200
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200201 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
202 linux_env_set("flash_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000203
Simon Glass64b723f2017-08-03 12:22:12 -0600204 cp = env_get("ethaddr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200205 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400206 linux_env_set("ethaddr", cp);
Jason McMullancfbe4572008-06-08 23:56:00 -0400207
Simon Glass64b723f2017-08-03 12:22:12 -0600208 cp = env_get("eth1addr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200209 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400210 linux_env_set("eth1addr", cp);
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200211
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100212 if (CONFIG_IS_ENABLED(MALTA)) {
Paul Burton43c4f592013-11-26 17:45:25 +0000213 sprintf(env_buf, "%un8r", gd->baudrate);
214 linux_env_set("modetty0", env_buf);
215 }
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100216}
217
218static int boot_reloc_ramdisk(bootm_headers_t *images)
219{
220 ulong rd_len = images->rd_end - images->rd_start;
221
222 /*
223 * In case of legacy uImage's, relocation of ramdisk is already done
224 * by do_bootm_states() and should not repeated in 'bootm prep'.
225 */
226 if (images->state & BOOTM_STATE_RAMDISK) {
227 debug("## Ramdisk already relocated\n");
228 return 0;
229 }
230
231 return boot_ramdisk_high(&images->lmb, images->rd_start,
232 rd_len, &images->initrd_start, &images->initrd_end);
233}
234
235static int boot_reloc_fdt(bootm_headers_t *images)
236{
237 /*
238 * In case of legacy uImage's, relocation of FDT is already done
239 * by do_bootm_states() and should not repeated in 'bootm prep'.
240 */
241 if (images->state & BOOTM_STATE_FDT) {
242 debug("## FDT already relocated\n");
243 return 0;
244 }
245
246#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
247 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
248 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
249 &images->ft_len);
250#else
251 return 0;
252#endif
253}
254
Alexey Brodkin91a31592018-01-24 20:47:09 +0300255#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
Purna Chandra Mandal432549f2016-04-18 18:31:38 +0530256int arch_fixup_fdt(void *blob)
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100257{
Purna Chandra Mandal432549f2016-04-18 18:31:38 +0530258 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100259 u64 mem_size = gd->ram_size;
260
261 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000262}
Alexey Brodkin91a31592018-01-24 20:47:09 +0300263#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000264
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100265static int boot_setup_fdt(bootm_headers_t *images)
266{
267 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
268 &images->lmb);
269}
270
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100271static void boot_prep_linux(bootm_headers_t *images)
272{
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100273 boot_reloc_ramdisk(images);
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +0100274
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100275 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
276 boot_reloc_fdt(images);
Daniel Schwierzeck8d7ff4d2015-01-14 21:44:13 +0100277 boot_setup_fdt(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100278 } else {
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100279 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
280 linux_cmdline_legacy(images);
281
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100282 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100283 linux_cmdline_append(images);
284
285 linux_cmdline_dump();
286 }
Zubair Lutfullah Kakakhel4c55ab42017-07-11 16:47:51 +0100287
288 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
289 linux_env_legacy(images);
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100290 }
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100291}
292
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000293static void boot_jump_linux(bootm_headers_t *images)
294{
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200295 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
296 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200297 ulong linux_extra = 0;
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000298
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200299 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000300
301 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
302
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100303 if (CONFIG_IS_ENABLED(MALTA))
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200304 linux_extra = gd->ram_size;
305
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100306#if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100307 bootstage_fdt_add_report();
308#endif
Daniel Schwierzeck735bb012015-11-01 17:36:15 +0100309#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Daniel Schwierzeck1a4797c2015-01-14 21:44:13 +0100310 bootstage_report();
311#endif
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000312
Daniel Schwierzeckd1b29d22015-02-22 16:58:30 +0100313 if (images->ft_len)
314 kernel(-2, (ulong)images->ft_addr, 0, 0);
315 else
316 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
317 linux_extra);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000318}
319
320int do_bootm_linux(int flag, int argc, char * const argv[],
321 bootm_headers_t *images)
322{
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000323 /* No need for those on MIPS */
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200324 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000325 return -1;
326
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100327 /*
328 * Cmdline init has been moved to 'bootm prep' because it has to be
329 * done after relocation of ramdisk to always pass correct values
330 * for rd_start and rd_size to Linux kernel.
331 */
332 if (flag & BOOTM_STATE_OS_CMDLINE)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200333 return 0;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200334
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000335 if (flag & BOOTM_STATE_OS_PREP) {
336 boot_prep_linux(images);
337 return 0;
338 }
339
Daniel Schwierzeck20a8b1c2015-11-01 17:36:14 +0100340 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000341 boot_jump_linux(images);
342 return 0;
343 }
Jason McMullancfbe4572008-06-08 23:56:00 -0400344
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100345 /* does not return */
Kumar Gala48626aa2008-08-15 08:24:45 -0500346 return 1;
wdenkbb1b8262003-03-27 12:09:35 +0000347}