blob: e0722d20d1e876122939117163071e4ecd525b26 [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 Schwierzecke51a6b72012-06-03 23:46:04 +020023static int linux_argc;
24static char **linux_argv;
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020025static char *linux_argp;
wdenkc0218802003-03-27 12:09:35 +000026
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020027static char **linux_env;
28static char *linux_env_p;
29static int linux_env_idx;
wdenkc0218802003-03-27 12:09:35 +000030
Daniel Schwierzeckf66cc1e2013-05-09 17:10:06 +020031static ulong arch_get_sp(void)
32{
33 ulong ret;
34
35 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
36
37 return ret;
38}
39
40void arch_lmb_reserve(struct lmb *lmb)
41{
42 ulong sp;
43
44 sp = arch_get_sp();
45 debug("## Current stack ends at 0x%08lx\n", sp);
46
47 /* adjust sp by 4K to be safe */
48 sp -= 4096;
49 lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
50}
51
Daniel Schwierzeckc9639422014-11-16 01:27:23 +010052static int boot_setup_linux(bootm_headers_t *images)
53{
54 int ret;
55 ulong rd_len;
56
57 rd_len = images->rd_end - images->rd_start;
58 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
59 rd_len, &images->initrd_start, &images->initrd_end);
60 if (ret)
61 return ret;
62
63 return 0;
64}
65
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020066static void linux_cmdline_init(void)
67{
68 linux_argc = 1;
69 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
70 linux_argv[0] = 0;
71 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
72}
73
74static void linux_cmdline_set(const char *value, size_t len)
75{
76 linux_argv[linux_argc] = linux_argp;
77 memcpy(linux_argp, value, len);
78 linux_argp[len] = 0;
79
80 linux_argp += len + 1;
81 linux_argc++;
82}
83
84static void linux_cmdline_dump(void)
85{
86 int i;
87
88 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
89 linux_argv, linux_argp);
90
91 for (i = 1; i < linux_argc; i++)
92 debug(" arg %03d: %s\n", i, linux_argv[i]);
93}
94
95static void boot_cmdline_linux(bootm_headers_t *images)
96{
97 const char *bootargs, *next, *quote;
98
99 linux_cmdline_init();
100
101 bootargs = getenv("bootargs");
102 if (!bootargs)
103 return;
104
105 next = bootargs;
106
107 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
108 quote = strchr(bootargs, '"');
109 next = strchr(bootargs, ' ');
110
111 while (next && quote && quote < next) {
112 /*
113 * we found a left quote before the next blank
114 * now we have to find the matching right quote
115 */
116 next = strchr(quote + 1, '"');
117 if (next) {
118 quote = strchr(next + 1, '"');
119 next = strchr(next + 1, ' ');
120 }
121 }
122
123 if (!next)
124 next = bootargs + strlen(bootargs);
125
126 linux_cmdline_set(bootargs, next - bootargs);
127
128 if (*next)
129 next++;
130
131 bootargs = next;
132 }
133
134 linux_cmdline_dump();
135}
136
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200137static void linux_env_init(void)
138{
139 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
140 linux_env[0] = 0;
141 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
142 linux_env_idx = 0;
143}
144
145static void linux_env_set(const char *env_name, const char *env_val)
146{
147 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
148 linux_env[linux_env_idx] = linux_env_p;
149
150 strcpy(linux_env_p, env_name);
151 linux_env_p += strlen(env_name);
152
Paul Burton7a9d1092013-11-09 10:22:08 +0000153 if (mips_boot_malta) {
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200154 linux_env_p++;
155 linux_env[++linux_env_idx] = linux_env_p;
156 } else {
157 *linux_env_p++ = '=';
158 }
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200159
160 strcpy(linux_env_p, env_val);
161 linux_env_p += strlen(env_val);
162
163 linux_env_p++;
164 linux_env[++linux_env_idx] = 0;
165 }
166}
167
Gabor Juhos0ea72132013-01-07 02:53:41 +0000168static void boot_prep_linux(bootm_headers_t *images)
wdenkc0218802003-03-27 12:09:35 +0000169{
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200170 char env_buf[12];
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200171 const char *cp;
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200172 ulong rd_start, rd_size;
wdenkc0218802003-03-27 12:09:35 +0000173
wdenk5da627a2003-10-09 20:09:04 +0000174#ifdef CONFIG_MEMSIZE_IN_BYTES
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200175 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
176 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
wdenk5da627a2003-10-09 20:09:04 +0000177#else
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200178 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
179 debug("## Giving linux memsize in MB, %lu\n",
Daniel Schwierzeck45bde482013-05-09 17:10:06 +0200180 (ulong)(gd->ram_size >> 20));
wdenk5da627a2003-10-09 20:09:04 +0000181#endif /* CONFIG_MEMSIZE_IN_BYTES */
wdenkc0218802003-03-27 12:09:35 +0000182
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200183 rd_start = UNCACHED_SDRAM(images->initrd_start);
184 rd_size = images->initrd_end - images->initrd_start;
185
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200186 linux_env_init();
187
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200188 linux_env_set("memsize", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000189
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200190 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200191 linux_env_set("initrd_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000192
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200193 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200194 linux_env_set("initrd_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000195
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200196 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
197 linux_env_set("flash_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000198
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200199 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
200 linux_env_set("flash_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000201
Jason McMullane7c37452008-06-08 23:56:00 -0400202 cp = getenv("ethaddr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200203 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400204 linux_env_set("ethaddr", cp);
Jason McMullane7c37452008-06-08 23:56:00 -0400205
206 cp = getenv("eth1addr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200207 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400208 linux_env_set("eth1addr", cp);
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200209
Paul Burtond18d49d2013-11-26 17:45:25 +0000210 if (mips_boot_malta) {
211 sprintf(env_buf, "%un8r", gd->baudrate);
212 linux_env_set("modetty0", env_buf);
213 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000214}
Jason McMullane7c37452008-06-08 23:56:00 -0400215
Gabor Juhos0ea72132013-01-07 02:53:41 +0000216static void boot_jump_linux(bootm_headers_t *images)
217{
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200218 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
219 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200220 ulong linux_extra = 0;
Gabor Juhos0ea72132013-01-07 02:53:41 +0000221
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200222 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000223
224 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
225
Paul Burton7a9d1092013-11-09 10:22:08 +0000226 if (mips_boot_malta)
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200227 linux_extra = gd->ram_size;
228
Gabor Juhos0ea72132013-01-07 02:53:41 +0000229 /* we assume that the kernel is in place */
230 printf("\nStarting kernel ...\n\n");
231
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200232 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000233}
234
235int do_bootm_linux(int flag, int argc, char * const argv[],
236 bootm_headers_t *images)
237{
Daniel Schwierzeckc9639422014-11-16 01:27:23 +0100238 int ret;
239
Gabor Juhos9c170e22013-01-07 02:53:42 +0000240 /* No need for those on MIPS */
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200241 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos9c170e22013-01-07 02:53:42 +0000242 return -1;
243
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200244 if (flag & BOOTM_STATE_OS_CMDLINE) {
245 boot_cmdline_linux(images);
246 return 0;
247 }
248
Gabor Juhos9c170e22013-01-07 02:53:42 +0000249 if (flag & BOOTM_STATE_OS_PREP) {
250 boot_prep_linux(images);
251 return 0;
252 }
253
254 if (flag & BOOTM_STATE_OS_GO) {
255 boot_jump_linux(images);
256 return 0;
257 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000258
Daniel Schwierzeckc9639422014-11-16 01:27:23 +0100259 ret = boot_setup_linux(images);
260 if (ret)
261 return ret;
262
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200263 boot_cmdline_linux(images);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000264 boot_prep_linux(images);
Gabor Juhose08634c2013-01-07 02:53:40 +0000265 boot_jump_linux(images);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200266
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100267 /* does not return */
Kumar Gala40d7e992008-08-15 08:24:45 -0500268 return 1;
wdenkc0218802003-03-27 12:09:35 +0000269}