blob: f947fd6c9e759bd54786c824f9826d21e371be3c [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>
9#include <command.h>
wdenkc0218802003-03-27 12:09:35 +000010#include <image.h>
Jean-Christophe PLAGNIOL-VILLARDa31e0912009-04-04 12:49:11 +020011#include <u-boot/zlib.h>
wdenkc0218802003-03-27 12:09:35 +000012#include <asm/byteorder.h>
13#include <asm/addrspace.h>
14
Wolfgang Denkd87080b2006-03-31 18:32:53 +020015DECLARE_GLOBAL_DATA_PTR;
16
wdenkc0218802003-03-27 12:09:35 +000017#define LINUX_MAX_ENVS 256
18#define LINUX_MAX_ARGS 256
19
Paul Burton7a9d1092013-11-09 10:22:08 +000020#if defined(CONFIG_MALTA)
21#define mips_boot_malta 1
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +020022#else
Paul Burton7a9d1092013-11-09 10:22:08 +000023#define mips_boot_malta 0
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +020024#endif
25
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020026static int linux_argc;
27static char **linux_argv;
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020028static char *linux_argp;
wdenkc0218802003-03-27 12:09:35 +000029
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020030static char **linux_env;
31static char *linux_env_p;
32static int linux_env_idx;
wdenkc0218802003-03-27 12:09:35 +000033
Daniel Schwierzeckf66cc1e2013-05-09 17:10:06 +020034static ulong arch_get_sp(void)
35{
36 ulong ret;
37
38 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
39
40 return ret;
41}
42
43void arch_lmb_reserve(struct lmb *lmb)
44{
45 ulong sp;
46
47 sp = arch_get_sp();
48 debug("## Current stack ends at 0x%08lx\n", sp);
49
50 /* adjust sp by 4K to be safe */
51 sp -= 4096;
52 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
53}
54
Daniel Schwierzeckc9639422014-11-16 01:27:23 +010055static int boot_setup_linux(bootm_headers_t *images)
56{
57 int ret;
58 ulong rd_len;
59
60 rd_len = images->rd_end - images->rd_start;
61 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
62 rd_len, &images->initrd_start, &images->initrd_end);
63 if (ret)
64 return ret;
65
66 return 0;
67}
68
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020069static void linux_cmdline_init(void)
70{
71 linux_argc = 1;
72 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
73 linux_argv[0] = 0;
74 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
75}
76
77static void linux_cmdline_set(const char *value, size_t len)
78{
79 linux_argv[linux_argc] = linux_argp;
80 memcpy(linux_argp, value, len);
81 linux_argp[len] = 0;
82
83 linux_argp += len + 1;
84 linux_argc++;
85}
86
87static void linux_cmdline_dump(void)
88{
89 int i;
90
91 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
92 linux_argv, linux_argp);
93
94 for (i = 1; i < linux_argc; i++)
95 debug(" arg %03d: %s\n", i, linux_argv[i]);
96}
97
98static void boot_cmdline_linux(bootm_headers_t *images)
99{
100 const char *bootargs, *next, *quote;
101
102 linux_cmdline_init();
103
104 bootargs = getenv("bootargs");
105 if (!bootargs)
106 return;
107
108 next = bootargs;
109
110 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
111 quote = strchr(bootargs, '"');
112 next = strchr(bootargs, ' ');
113
114 while (next && quote && quote < next) {
115 /*
116 * we found a left quote before the next blank
117 * now we have to find the matching right quote
118 */
119 next = strchr(quote + 1, '"');
120 if (next) {
121 quote = strchr(next + 1, '"');
122 next = strchr(next + 1, ' ');
123 }
124 }
125
126 if (!next)
127 next = bootargs + strlen(bootargs);
128
129 linux_cmdline_set(bootargs, next - bootargs);
130
131 if (*next)
132 next++;
133
134 bootargs = next;
135 }
136
137 linux_cmdline_dump();
138}
139
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200140static void linux_env_init(void)
141{
142 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
143 linux_env[0] = 0;
144 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
145 linux_env_idx = 0;
146}
147
148static void linux_env_set(const char *env_name, const char *env_val)
149{
150 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
151 linux_env[linux_env_idx] = linux_env_p;
152
153 strcpy(linux_env_p, env_name);
154 linux_env_p += strlen(env_name);
155
Paul Burton7a9d1092013-11-09 10:22:08 +0000156 if (mips_boot_malta) {
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200157 linux_env_p++;
158 linux_env[++linux_env_idx] = linux_env_p;
159 } else {
160 *linux_env_p++ = '=';
161 }
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200162
163 strcpy(linux_env_p, env_val);
164 linux_env_p += strlen(env_val);
165
166 linux_env_p++;
167 linux_env[++linux_env_idx] = 0;
168 }
169}
170
Gabor Juhos0ea72132013-01-07 02:53:41 +0000171static void boot_prep_linux(bootm_headers_t *images)
wdenkc0218802003-03-27 12:09:35 +0000172{
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200173 char env_buf[12];
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200174 const char *cp;
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200175 ulong rd_start, rd_size;
wdenkc0218802003-03-27 12:09:35 +0000176
wdenk5da627a2003-10-09 20:09:04 +0000177#ifdef CONFIG_MEMSIZE_IN_BYTES
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200178 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
179 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
wdenk5da627a2003-10-09 20:09:04 +0000180#else
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200181 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
182 debug("## Giving linux memsize in MB, %lu\n",
Daniel Schwierzeck45bde482013-05-09 17:10:06 +0200183 (ulong)(gd->ram_size >> 20));
wdenk5da627a2003-10-09 20:09:04 +0000184#endif /* CONFIG_MEMSIZE_IN_BYTES */
wdenkc0218802003-03-27 12:09:35 +0000185
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200186 rd_start = UNCACHED_SDRAM(images->initrd_start);
187 rd_size = images->initrd_end - images->initrd_start;
188
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200189 linux_env_init();
190
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200191 linux_env_set("memsize", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000192
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200193 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200194 linux_env_set("initrd_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000195
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200196 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200197 linux_env_set("initrd_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000198
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200199 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
200 linux_env_set("flash_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000201
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200202 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
203 linux_env_set("flash_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000204
Jason McMullane7c37452008-06-08 23:56:00 -0400205 cp = getenv("ethaddr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200206 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400207 linux_env_set("ethaddr", cp);
Jason McMullane7c37452008-06-08 23:56:00 -0400208
209 cp = getenv("eth1addr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200210 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400211 linux_env_set("eth1addr", cp);
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200212
Paul Burtond18d49d2013-11-26 17:45:25 +0000213 if (mips_boot_malta) {
214 sprintf(env_buf, "%un8r", gd->baudrate);
215 linux_env_set("modetty0", env_buf);
216 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000217}
Jason McMullane7c37452008-06-08 23:56:00 -0400218
Gabor Juhos0ea72132013-01-07 02:53:41 +0000219static void boot_jump_linux(bootm_headers_t *images)
220{
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200221 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
222 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200223 ulong linux_extra = 0;
Gabor Juhos0ea72132013-01-07 02:53:41 +0000224
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200225 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000226
227 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
228
Paul Burton7a9d1092013-11-09 10:22:08 +0000229 if (mips_boot_malta)
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200230 linux_extra = gd->ram_size;
231
Gabor Juhos0ea72132013-01-07 02:53:41 +0000232 /* we assume that the kernel is in place */
233 printf("\nStarting kernel ...\n\n");
234
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200235 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000236}
237
238int do_bootm_linux(int flag, int argc, char * const argv[],
239 bootm_headers_t *images)
240{
Daniel Schwierzeckc9639422014-11-16 01:27:23 +0100241 int ret;
242
Gabor Juhos9c170e22013-01-07 02:53:42 +0000243 /* No need for those on MIPS */
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200244 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos9c170e22013-01-07 02:53:42 +0000245 return -1;
246
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200247 if (flag & BOOTM_STATE_OS_CMDLINE) {
248 boot_cmdline_linux(images);
249 return 0;
250 }
251
Gabor Juhos9c170e22013-01-07 02:53:42 +0000252 if (flag & BOOTM_STATE_OS_PREP) {
253 boot_prep_linux(images);
254 return 0;
255 }
256
257 if (flag & BOOTM_STATE_OS_GO) {
258 boot_jump_linux(images);
259 return 0;
260 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000261
Daniel Schwierzeckc9639422014-11-16 01:27:23 +0100262 ret = boot_setup_linux(images);
263 if (ret)
264 return ret;
265
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200266 boot_cmdline_linux(images);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000267 boot_prep_linux(images);
Gabor Juhose08634c2013-01-07 02:53:40 +0000268 boot_jump_linux(images);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200269
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100270 /* does not return */
Kumar Gala40d7e992008-08-15 08:24:45 -0500271 return 1;
wdenkc0218802003-03-27 12:09:35 +0000272}