blob: a028a47ab9b5f32691ef354927fe818b92365bbf [file] [log] [blame]
wdenkc0218802003-03-27 12:09:35 +00001/*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc0218802003-03-27 12:09:35 +00006 */
7
8#include <common.h>
wdenkc0218802003-03-27 12:09:35 +00009#include <image.h>
wdenkc0218802003-03-27 12:09:35 +000010#include <asm/addrspace.h>
11
Wolfgang Denkd87080b2006-03-31 18:32:53 +020012DECLARE_GLOBAL_DATA_PTR;
13
wdenkc0218802003-03-27 12:09:35 +000014#define LINUX_MAX_ENVS 256
15#define LINUX_MAX_ARGS 256
16
Paul Burton7a9d1092013-11-09 10:22:08 +000017#if defined(CONFIG_MALTA)
18#define mips_boot_malta 1
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +020019#else
Paul Burton7a9d1092013-11-09 10:22:08 +000020#define mips_boot_malta 0
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +020021#endif
22
Daniel Schwierzeck25fc6642015-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 Schwierzecke51a6b72012-06-03 23:46:04 +020029static int linux_argc;
30static char **linux_argv;
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020031static char *linux_argp;
wdenkc0218802003-03-27 12:09:35 +000032
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020033static char **linux_env;
34static char *linux_env_p;
35static int linux_env_idx;
wdenkc0218802003-03-27 12:09:35 +000036
Daniel Schwierzeckf66cc1e2013-05-09 17:10:06 +020037static ulong arch_get_sp(void)
38{
39 ulong ret;
40
41 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
42
43 return ret;
44}
45
46void arch_lmb_reserve(struct lmb *lmb)
47{
48 ulong sp;
49
50 sp = arch_get_sp();
51 debug("## Current stack ends at 0x%08lx\n", sp);
52
53 /* adjust sp by 4K to be safe */
54 sp -= 4096;
55 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
56}
57
Daniel Schwierzeckc9639422014-11-16 01:27:23 +010058static int boot_setup_linux(bootm_headers_t *images)
59{
60 int ret;
61 ulong rd_len;
62
63 rd_len = images->rd_end - images->rd_start;
64 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
65 rd_len, &images->initrd_start, &images->initrd_end);
66 if (ret)
67 return ret;
68
69 return 0;
70}
71
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020072static void linux_cmdline_init(void)
73{
74 linux_argc = 1;
75 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
76 linux_argv[0] = 0;
77 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
78}
79
80static void linux_cmdline_set(const char *value, size_t len)
81{
82 linux_argv[linux_argc] = linux_argp;
83 memcpy(linux_argp, value, len);
84 linux_argp[len] = 0;
85
86 linux_argp += len + 1;
87 linux_argc++;
88}
89
90static void linux_cmdline_dump(void)
91{
92 int i;
93
94 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
95 linux_argv, linux_argp);
96
97 for (i = 1; i < linux_argc; i++)
98 debug(" arg %03d: %s\n", i, linux_argv[i]);
99}
100
Daniel Schwierzeck25fc6642015-01-14 21:44:13 +0100101static void linux_cmdline_legacy(bootm_headers_t *images)
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200102{
103 const char *bootargs, *next, *quote;
104
105 linux_cmdline_init();
106
107 bootargs = getenv("bootargs");
108 if (!bootargs)
109 return;
110
111 next = bootargs;
112
113 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
114 quote = strchr(bootargs, '"');
115 next = strchr(bootargs, ' ');
116
117 while (next && quote && quote < next) {
118 /*
119 * we found a left quote before the next blank
120 * now we have to find the matching right quote
121 */
122 next = strchr(quote + 1, '"');
123 if (next) {
124 quote = strchr(next + 1, '"');
125 next = strchr(next + 1, ' ');
126 }
127 }
128
129 if (!next)
130 next = bootargs + strlen(bootargs);
131
132 linux_cmdline_set(bootargs, next - bootargs);
133
134 if (*next)
135 next++;
136
137 bootargs = next;
138 }
Daniel Schwierzeck25fc6642015-01-14 21:44:13 +0100139}
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200140
Daniel Schwierzeck25fc6642015-01-14 21:44:13 +0100141static void boot_cmdline_linux(bootm_headers_t *images)
142{
143 if (mips_boot_cmdline_legacy) {
144 linux_cmdline_legacy(images);
145 linux_cmdline_dump();
146 }
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200147}
148
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200149static void linux_env_init(void)
150{
151 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
152 linux_env[0] = 0;
153 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
154 linux_env_idx = 0;
155}
156
157static void linux_env_set(const char *env_name, const char *env_val)
158{
159 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
160 linux_env[linux_env_idx] = linux_env_p;
161
162 strcpy(linux_env_p, env_name);
163 linux_env_p += strlen(env_name);
164
Paul Burton7a9d1092013-11-09 10:22:08 +0000165 if (mips_boot_malta) {
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200166 linux_env_p++;
167 linux_env[++linux_env_idx] = linux_env_p;
168 } else {
169 *linux_env_p++ = '=';
170 }
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200171
172 strcpy(linux_env_p, env_val);
173 linux_env_p += strlen(env_val);
174
175 linux_env_p++;
176 linux_env[++linux_env_idx] = 0;
177 }
178}
179
Gabor Juhos0ea72132013-01-07 02:53:41 +0000180static void boot_prep_linux(bootm_headers_t *images)
wdenkc0218802003-03-27 12:09:35 +0000181{
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200182 char env_buf[12];
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200183 const char *cp;
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200184 ulong rd_start, rd_size;
wdenkc0218802003-03-27 12:09:35 +0000185
wdenk5da627a2003-10-09 20:09:04 +0000186#ifdef CONFIG_MEMSIZE_IN_BYTES
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200187 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
188 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
wdenk5da627a2003-10-09 20:09:04 +0000189#else
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200190 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
191 debug("## Giving linux memsize in MB, %lu\n",
Daniel Schwierzeck45bde482013-05-09 17:10:06 +0200192 (ulong)(gd->ram_size >> 20));
wdenk5da627a2003-10-09 20:09:04 +0000193#endif /* CONFIG_MEMSIZE_IN_BYTES */
wdenkc0218802003-03-27 12:09:35 +0000194
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200195 rd_start = UNCACHED_SDRAM(images->initrd_start);
196 rd_size = images->initrd_end - images->initrd_start;
197
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200198 linux_env_init();
199
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200200 linux_env_set("memsize", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000201
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200202 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200203 linux_env_set("initrd_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000204
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200205 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200206 linux_env_set("initrd_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000207
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200208 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
209 linux_env_set("flash_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000210
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200211 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
212 linux_env_set("flash_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000213
Jason McMullane7c37452008-06-08 23:56:00 -0400214 cp = getenv("ethaddr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200215 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400216 linux_env_set("ethaddr", cp);
Jason McMullane7c37452008-06-08 23:56:00 -0400217
218 cp = getenv("eth1addr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200219 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400220 linux_env_set("eth1addr", cp);
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200221
Paul Burtond18d49d2013-11-26 17:45:25 +0000222 if (mips_boot_malta) {
223 sprintf(env_buf, "%un8r", gd->baudrate);
224 linux_env_set("modetty0", env_buf);
225 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000226}
Jason McMullane7c37452008-06-08 23:56:00 -0400227
Gabor Juhos0ea72132013-01-07 02:53:41 +0000228static void boot_jump_linux(bootm_headers_t *images)
229{
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200230 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
231 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200232 ulong linux_extra = 0;
Gabor Juhos0ea72132013-01-07 02:53:41 +0000233
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200234 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000235
236 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
237
Paul Burton7a9d1092013-11-09 10:22:08 +0000238 if (mips_boot_malta)
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200239 linux_extra = gd->ram_size;
240
Gabor Juhos0ea72132013-01-07 02:53:41 +0000241 /* we assume that the kernel is in place */
242 printf("\nStarting kernel ...\n\n");
243
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200244 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000245}
246
247int do_bootm_linux(int flag, int argc, char * const argv[],
248 bootm_headers_t *images)
249{
Daniel Schwierzeckc9639422014-11-16 01:27:23 +0100250 int ret;
251
Gabor Juhos9c170e22013-01-07 02:53:42 +0000252 /* No need for those on MIPS */
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200253 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos9c170e22013-01-07 02:53:42 +0000254 return -1;
255
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200256 if (flag & BOOTM_STATE_OS_CMDLINE) {
257 boot_cmdline_linux(images);
258 return 0;
259 }
260
Gabor Juhos9c170e22013-01-07 02:53:42 +0000261 if (flag & BOOTM_STATE_OS_PREP) {
262 boot_prep_linux(images);
263 return 0;
264 }
265
266 if (flag & BOOTM_STATE_OS_GO) {
267 boot_jump_linux(images);
268 return 0;
269 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000270
Daniel Schwierzeckc9639422014-11-16 01:27:23 +0100271 ret = boot_setup_linux(images);
272 if (ret)
273 return ret;
274
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200275 boot_cmdline_linux(images);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000276 boot_prep_linux(images);
Gabor Juhose08634c2013-01-07 02:53:40 +0000277 boot_jump_linux(images);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200278
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100279 /* does not return */
Kumar Gala40d7e992008-08-15 08:24:45 -0500280 return 1;
wdenkc0218802003-03-27 12:09:35 +0000281}