blob: 0a13f6edb7554a388d9f7a3cf82fa051b7154b19 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc0218802003-03-27 12:09:35 +00002/*
3 * (C) Copyright 2003
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkc0218802003-03-27 12:09:35 +00005 */
6
7#include <common.h>
Simon Glass52f24232020-05-10 11:40:00 -06008#include <bootstage.h>
Simon Glass4bfd1f52019-08-01 09:46:43 -06009#include <env.h>
wdenkc0218802003-03-27 12:09:35 +000010#include <image.h>
Daniel Schwierzeck5002d8c2015-01-14 21:44:13 +010011#include <fdt_support.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060012#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
wdenkc0218802003-03-27 12:09:35 +000014#include <asm/addrspace.h>
Purna Chandra Mandalfdff5b02016-04-18 18:31:38 +053015#include <asm/io.h>
wdenkc0218802003-03-27 12:09:35 +000016
Wolfgang Denkd87080b2006-03-31 18:32:53 +020017DECLARE_GLOBAL_DATA_PTR;
18
wdenkc0218802003-03-27 12:09:35 +000019#define LINUX_MAX_ENVS 256
20#define LINUX_MAX_ARGS 256
21
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020022static int linux_argc;
23static char **linux_argv;
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020024static char *linux_argp;
wdenkc0218802003-03-27 12:09:35 +000025
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020026static char **linux_env;
27static char *linux_env_p;
28static int linux_env_idx;
wdenkc0218802003-03-27 12:09:35 +000029
Daniel Schwierzeckf66cc1e2013-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{
41 ulong sp;
42
43 sp = arch_get_sp();
44 debug("## Current stack ends at 0x%08lx\n", sp);
45
46 /* adjust sp by 4K to be safe */
47 sp -= 4096;
Paul Burton7a3e0f72016-09-26 19:28:56 +010048 lmb_reserve(lmb, sp, gd->ram_top - sp);
Daniel Schwierzeckf66cc1e2013-05-09 17:10:06 +020049}
50
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020051static void linux_cmdline_init(void)
52{
53 linux_argc = 1;
Daniel Schwierzeckdd1bb422020-07-12 01:46:15 +020054 linux_argv = (char **)CKSEG1ADDR(gd->bd->bi_boot_params);
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020055 linux_argv[0] = 0;
56 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
57}
58
59static void linux_cmdline_set(const char *value, size_t len)
60{
61 linux_argv[linux_argc] = linux_argp;
62 memcpy(linux_argp, value, len);
63 linux_argp[len] = 0;
64
65 linux_argp += len + 1;
66 linux_argc++;
67}
68
69static void linux_cmdline_dump(void)
70{
71 int i;
72
73 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
74 linux_argv, linux_argp);
75
76 for (i = 1; i < linux_argc; i++)
77 debug(" arg %03d: %s\n", i, linux_argv[i]);
78}
79
Daniel Schwierzeck25fc6642015-01-14 21:44:13 +010080static void linux_cmdline_legacy(bootm_headers_t *images)
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020081{
82 const char *bootargs, *next, *quote;
83
84 linux_cmdline_init();
85
Simon Glass00caae62017-08-03 12:22:12 -060086 bootargs = env_get("bootargs");
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020087 if (!bootargs)
88 return;
89
90 next = bootargs;
91
92 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
93 quote = strchr(bootargs, '"');
94 next = strchr(bootargs, ' ');
95
96 while (next && quote && quote < next) {
97 /*
98 * we found a left quote before the next blank
99 * now we have to find the matching right quote
100 */
101 next = strchr(quote + 1, '"');
102 if (next) {
103 quote = strchr(next + 1, '"');
104 next = strchr(next + 1, ' ');
105 }
106 }
107
108 if (!next)
109 next = bootargs + strlen(bootargs);
110
111 linux_cmdline_set(bootargs, next - bootargs);
112
113 if (*next)
114 next++;
115
116 bootargs = next;
117 }
Daniel Schwierzeck25fc6642015-01-14 21:44:13 +0100118}
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200119
Daniel Schwierzeck8cec7252015-01-14 21:44:13 +0100120static void linux_cmdline_append(bootm_headers_t *images)
121{
122 char buf[24];
123 ulong mem, rd_start, rd_size;
124
125 /* append mem */
126 mem = gd->ram_size >> 20;
127 sprintf(buf, "mem=%luM", mem);
128 linux_cmdline_set(buf, strlen(buf));
129
130 /* append rd_start and rd_size */
131 rd_start = images->initrd_start;
132 rd_size = images->initrd_end - images->initrd_start;
133
134 if (rd_size) {
135 sprintf(buf, "rd_start=0x%08lX", rd_start);
136 linux_cmdline_set(buf, strlen(buf));
137 sprintf(buf, "rd_size=0x%lX", rd_size);
138 linux_cmdline_set(buf, strlen(buf));
139 }
140}
141
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200142static void linux_env_init(void)
143{
144 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
145 linux_env[0] = 0;
146 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
147 linux_env_idx = 0;
148}
149
150static void linux_env_set(const char *env_name, const char *env_val)
151{
152 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
153 linux_env[linux_env_idx] = linux_env_p;
154
155 strcpy(linux_env_p, env_name);
156 linux_env_p += strlen(env_name);
157
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100158 if (CONFIG_IS_ENABLED(MALTA)) {
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200159 linux_env_p++;
160 linux_env[++linux_env_idx] = linux_env_p;
161 } else {
162 *linux_env_p++ = '=';
163 }
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200164
165 strcpy(linux_env_p, env_val);
166 linux_env_p += strlen(env_val);
167
168 linux_env_p++;
169 linux_env[++linux_env_idx] = 0;
170 }
171}
172
Daniel Schwierzeckca65e582015-01-14 21:44:13 +0100173static void linux_env_legacy(bootm_headers_t *images)
wdenkc0218802003-03-27 12:09:35 +0000174{
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200175 char env_buf[12];
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200176 const char *cp;
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200177 ulong rd_start, rd_size;
wdenkc0218802003-03-27 12:09:35 +0000178
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100179 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
180 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
181 debug("## Giving linux memsize in bytes, %lu\n",
182 (ulong)gd->ram_size);
183 } else {
184 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
185 debug("## Giving linux memsize in MB, %lu\n",
186 (ulong)(gd->ram_size >> 20));
187 }
wdenkc0218802003-03-27 12:09:35 +0000188
Daniel Schwierzeckdd1bb422020-07-12 01:46:15 +0200189 rd_start = CKSEG1ADDR(images->initrd_start);
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200190 rd_size = images->initrd_end - images->initrd_start;
191
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200192 linux_env_init();
193
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200194 linux_env_set("memsize", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000195
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200196 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200197 linux_env_set("initrd_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000198
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200199 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200200 linux_env_set("initrd_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000201
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200202 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
203 linux_env_set("flash_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000204
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200205 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
206 linux_env_set("flash_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000207
Simon Glass00caae62017-08-03 12:22:12 -0600208 cp = env_get("ethaddr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200209 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400210 linux_env_set("ethaddr", cp);
Jason McMullane7c37452008-06-08 23:56:00 -0400211
Simon Glass00caae62017-08-03 12:22:12 -0600212 cp = env_get("eth1addr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200213 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400214 linux_env_set("eth1addr", cp);
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200215
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100216 if (CONFIG_IS_ENABLED(MALTA)) {
Paul Burtond18d49d2013-11-26 17:45:25 +0000217 sprintf(env_buf, "%un8r", gd->baudrate);
218 linux_env_set("modetty0", env_buf);
219 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000220}
Jason McMullane7c37452008-06-08 23:56:00 -0400221
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100222static int boot_reloc_fdt(bootm_headers_t *images)
223{
224 /*
225 * In case of legacy uImage's, relocation of FDT is already done
226 * by do_bootm_states() and should not repeated in 'bootm prep'.
227 */
228 if (images->state & BOOTM_STATE_FDT) {
229 debug("## FDT already relocated\n");
230 return 0;
231 }
232
233#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
234 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
235 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
236 &images->ft_len);
237#else
238 return 0;
239#endif
240}
241
Alexey Brodkin42803422018-01-24 20:47:09 +0300242#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
Purna Chandra Mandalfdff5b02016-04-18 18:31:38 +0530243int arch_fixup_fdt(void *blob)
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100244{
Purna Chandra Mandalfdff5b02016-04-18 18:31:38 +0530245 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100246 u64 mem_size = gd->ram_size;
247
248 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100249}
Alexey Brodkin42803422018-01-24 20:47:09 +0300250#endif
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100251
252static int boot_setup_fdt(bootm_headers_t *images)
253{
Horatiu Vultur6943cc92019-04-24 17:21:29 +0200254 images->initrd_start = virt_to_phys((void *)images->initrd_start);
255 images->initrd_end = virt_to_phys((void *)images->initrd_end);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100256 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
257 &images->lmb);
258}
259
Daniel Schwierzeckca65e582015-01-14 21:44:13 +0100260static void boot_prep_linux(bootm_headers_t *images)
261{
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100262 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
263 boot_reloc_fdt(images);
Daniel Schwierzeck5002d8c2015-01-14 21:44:13 +0100264 boot_setup_fdt(images);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100265 } else {
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100266 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
267 linux_cmdline_legacy(images);
268
Zubair Lutfullah Kakakhel48bfc312017-07-11 16:47:51 +0100269 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100270 linux_cmdline_append(images);
271
272 linux_cmdline_dump();
273 }
Zubair Lutfullah Kakakhel48bfc312017-07-11 16:47:51 +0100274
275 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
276 linux_env_legacy(images);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100277 }
Daniel Schwierzeckca65e582015-01-14 21:44:13 +0100278}
279
Gabor Juhos0ea72132013-01-07 02:53:41 +0000280static void boot_jump_linux(bootm_headers_t *images)
281{
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200282 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
283 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200284 ulong linux_extra = 0;
Gabor Juhos0ea72132013-01-07 02:53:41 +0000285
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200286 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000287
288 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
289
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100290 if (CONFIG_IS_ENABLED(MALTA))
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200291 linux_extra = gd->ram_size;
292
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100293#if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
Daniel Schwierzecke13a50b2015-01-14 21:44:13 +0100294 bootstage_fdt_add_report();
295#endif
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100296#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Daniel Schwierzecke13a50b2015-01-14 21:44:13 +0100297 bootstage_report();
298#endif
Gabor Juhos0ea72132013-01-07 02:53:41 +0000299
Weijie Gao71059732020-04-21 09:28:25 +0200300 if (CONFIG_IS_ENABLED(RESTORE_EXCEPTION_VECTOR_BASE))
301 trap_restore();
302
Daniel Schwierzeck90b1c9f2015-02-22 16:58:30 +0100303 if (images->ft_len)
304 kernel(-2, (ulong)images->ft_addr, 0, 0);
305 else
306 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
307 linux_extra);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000308}
309
Simon Glass09140112020-05-10 11:40:03 -0600310int do_bootm_linux(int flag, int argc, char *const argv[],
311 bootm_headers_t *images)
Gabor Juhos0ea72132013-01-07 02:53:41 +0000312{
Gabor Juhos9c170e22013-01-07 02:53:42 +0000313 /* No need for those on MIPS */
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200314 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos9c170e22013-01-07 02:53:42 +0000315 return -1;
316
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100317 /*
318 * Cmdline init has been moved to 'bootm prep' because it has to be
319 * done after relocation of ramdisk to always pass correct values
320 * for rd_start and rd_size to Linux kernel.
321 */
322 if (flag & BOOTM_STATE_OS_CMDLINE)
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200323 return 0;
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200324
Gabor Juhos9c170e22013-01-07 02:53:42 +0000325 if (flag & BOOTM_STATE_OS_PREP) {
326 boot_prep_linux(images);
327 return 0;
328 }
329
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100330 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
Gabor Juhos9c170e22013-01-07 02:53:42 +0000331 boot_jump_linux(images);
332 return 0;
333 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000334
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100335 /* does not return */
Kumar Gala40d7e992008-08-15 08:24:45 -0500336 return 1;
wdenkc0218802003-03-27 12:09:35 +0000337}