blob: fa579b36e49709fad660f09d82b82656e2fd1c85 [file] [log] [blame]
wdenkbb1b8262003-03-27 12:09:35 +00001/*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkbb1b8262003-03-27 12:09:35 +00006 */
7
8#include <common.h>
wdenkbb1b8262003-03-27 12:09:35 +00009#include <image.h>
wdenkbb1b8262003-03-27 12:09:35 +000010#include <asm/addrspace.h>
11
Wolfgang Denk6405a152006-03-31 18:32:53 +020012DECLARE_GLOBAL_DATA_PTR;
13
wdenkbb1b8262003-03-27 12:09:35 +000014#define LINUX_MAX_ENVS 256
15#define LINUX_MAX_ARGS 256
16
Paul Burton10a74b52013-11-09 10:22:08 +000017#if defined(CONFIG_MALTA)
18#define mips_boot_malta 1
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +020019#else
Paul Burton10a74b52013-11-09 10:22:08 +000020#define mips_boot_malta 0
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +020021#endif
22
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +010023#if defined(CONFIG_MIPS_BOOT_CMDLINE_LEGACY)
24#define mips_boot_cmdline_legacy 1
25#else
26#define mips_boot_cmdline_legacy 0
27#endif
28
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +010029#if defined(CONFIG_MIPS_BOOT_ENV_LEGACY)
30#define mips_boot_env_legacy 1
31#else
32#define mips_boot_env_legacy 0
33#endif
34
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020035static int linux_argc;
36static char **linux_argv;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020037static char *linux_argp;
wdenkbb1b8262003-03-27 12:09:35 +000038
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020039static char **linux_env;
40static char *linux_env_p;
41static int linux_env_idx;
wdenkbb1b8262003-03-27 12:09:35 +000042
Daniel Schwierzeck22d1c232013-05-09 17:10:06 +020043static ulong arch_get_sp(void)
44{
45 ulong ret;
46
47 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
48
49 return ret;
50}
51
52void arch_lmb_reserve(struct lmb *lmb)
53{
54 ulong sp;
55
56 sp = arch_get_sp();
57 debug("## Current stack ends at 0x%08lx\n", sp);
58
59 /* adjust sp by 4K to be safe */
60 sp -= 4096;
61 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
62}
63
Daniel Schwierzeck06658382014-11-16 01:27:23 +010064static int boot_setup_linux(bootm_headers_t *images)
65{
66 int ret;
67 ulong rd_len;
68
69 rd_len = images->rd_end - images->rd_start;
70 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
71 rd_len, &images->initrd_start, &images->initrd_end);
72 if (ret)
73 return ret;
74
75 return 0;
76}
77
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020078static void linux_cmdline_init(void)
79{
80 linux_argc = 1;
81 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
82 linux_argv[0] = 0;
83 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
84}
85
86static void linux_cmdline_set(const char *value, size_t len)
87{
88 linux_argv[linux_argc] = linux_argp;
89 memcpy(linux_argp, value, len);
90 linux_argp[len] = 0;
91
92 linux_argp += len + 1;
93 linux_argc++;
94}
95
96static void linux_cmdline_dump(void)
97{
98 int i;
99
100 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
101 linux_argv, linux_argp);
102
103 for (i = 1; i < linux_argc; i++)
104 debug(" arg %03d: %s\n", i, linux_argv[i]);
105}
106
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +0100107static void linux_cmdline_legacy(bootm_headers_t *images)
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200108{
109 const char *bootargs, *next, *quote;
110
111 linux_cmdline_init();
112
113 bootargs = getenv("bootargs");
114 if (!bootargs)
115 return;
116
117 next = bootargs;
118
119 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
120 quote = strchr(bootargs, '"');
121 next = strchr(bootargs, ' ');
122
123 while (next && quote && quote < next) {
124 /*
125 * we found a left quote before the next blank
126 * now we have to find the matching right quote
127 */
128 next = strchr(quote + 1, '"');
129 if (next) {
130 quote = strchr(next + 1, '"');
131 next = strchr(next + 1, ' ');
132 }
133 }
134
135 if (!next)
136 next = bootargs + strlen(bootargs);
137
138 linux_cmdline_set(bootargs, next - bootargs);
139
140 if (*next)
141 next++;
142
143 bootargs = next;
144 }
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +0100145}
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200146
Daniel Schwierzeckf9749fa2015-01-14 21:44:13 +0100147static void boot_cmdline_linux(bootm_headers_t *images)
148{
149 if (mips_boot_cmdline_legacy) {
150 linux_cmdline_legacy(images);
151 linux_cmdline_dump();
152 }
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200153}
154
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200155static void linux_env_init(void)
156{
157 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
158 linux_env[0] = 0;
159 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
160 linux_env_idx = 0;
161}
162
163static void linux_env_set(const char *env_name, const char *env_val)
164{
165 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
166 linux_env[linux_env_idx] = linux_env_p;
167
168 strcpy(linux_env_p, env_name);
169 linux_env_p += strlen(env_name);
170
Paul Burton10a74b52013-11-09 10:22:08 +0000171 if (mips_boot_malta) {
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200172 linux_env_p++;
173 linux_env[++linux_env_idx] = linux_env_p;
174 } else {
175 *linux_env_p++ = '=';
176 }
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200177
178 strcpy(linux_env_p, env_val);
179 linux_env_p += strlen(env_val);
180
181 linux_env_p++;
182 linux_env[++linux_env_idx] = 0;
183 }
184}
185
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100186static void linux_env_legacy(bootm_headers_t *images)
wdenkbb1b8262003-03-27 12:09:35 +0000187{
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200188 char env_buf[12];
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200189 const char *cp;
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200190 ulong rd_start, rd_size;
wdenkbb1b8262003-03-27 12:09:35 +0000191
wdenk9b7f3842003-10-09 20:09:04 +0000192#ifdef CONFIG_MEMSIZE_IN_BYTES
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200193 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
194 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
wdenk9b7f3842003-10-09 20:09:04 +0000195#else
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200196 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
197 debug("## Giving linux memsize in MB, %lu\n",
Daniel Schwierzeck40f30c02013-05-09 17:10:06 +0200198 (ulong)(gd->ram_size >> 20));
wdenk9b7f3842003-10-09 20:09:04 +0000199#endif /* CONFIG_MEMSIZE_IN_BYTES */
wdenkbb1b8262003-03-27 12:09:35 +0000200
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200201 rd_start = UNCACHED_SDRAM(images->initrd_start);
202 rd_size = images->initrd_end - images->initrd_start;
203
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200204 linux_env_init();
205
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200206 linux_env_set("memsize", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000207
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200208 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200209 linux_env_set("initrd_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000210
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200211 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200212 linux_env_set("initrd_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000213
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200214 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
215 linux_env_set("flash_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000216
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200217 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
218 linux_env_set("flash_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000219
Jason McMullancfbe4572008-06-08 23:56:00 -0400220 cp = getenv("ethaddr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200221 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400222 linux_env_set("ethaddr", cp);
Jason McMullancfbe4572008-06-08 23:56:00 -0400223
224 cp = getenv("eth1addr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200225 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400226 linux_env_set("eth1addr", cp);
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200227
Paul Burton43c4f592013-11-26 17:45:25 +0000228 if (mips_boot_malta) {
229 sprintf(env_buf, "%un8r", gd->baudrate);
230 linux_env_set("modetty0", env_buf);
231 }
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000232}
233
Daniel Schwierzeckc07dc602015-01-14 21:44:13 +0100234static void boot_prep_linux(bootm_headers_t *images)
235{
236 if (mips_boot_env_legacy)
237 linux_env_legacy(images);
238}
239
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000240static void boot_jump_linux(bootm_headers_t *images)
241{
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200242 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
243 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200244 ulong linux_extra = 0;
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000245
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200246 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000247
248 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
249
Paul Burton10a74b52013-11-09 10:22:08 +0000250 if (mips_boot_malta)
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200251 linux_extra = gd->ram_size;
252
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000253 /* we assume that the kernel is in place */
254 printf("\nStarting kernel ...\n\n");
255
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200256 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000257}
258
259int do_bootm_linux(int flag, int argc, char * const argv[],
260 bootm_headers_t *images)
261{
Daniel Schwierzeck06658382014-11-16 01:27:23 +0100262 int ret;
263
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000264 /* No need for those on MIPS */
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200265 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000266 return -1;
267
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200268 if (flag & BOOTM_STATE_OS_CMDLINE) {
269 boot_cmdline_linux(images);
270 return 0;
271 }
272
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000273 if (flag & BOOTM_STATE_OS_PREP) {
274 boot_prep_linux(images);
275 return 0;
276 }
277
278 if (flag & BOOTM_STATE_OS_GO) {
279 boot_jump_linux(images);
280 return 0;
281 }
Jason McMullancfbe4572008-06-08 23:56:00 -0400282
Daniel Schwierzeck06658382014-11-16 01:27:23 +0100283 ret = boot_setup_linux(images);
284 if (ret)
285 return ret;
286
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200287 boot_cmdline_linux(images);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000288 boot_prep_linux(images);
Gabor Juhose0d783b2013-01-07 02:53:40 +0000289 boot_jump_linux(images);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200290
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100291 /* does not return */
Kumar Gala48626aa2008-08-15 08:24:45 -0500292 return 1;
wdenkbb1b8262003-03-27 12:09:35 +0000293}