blob: 66340ea47046319083cdfc0556b5dc292681b980 [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>
9#include <command.h>
wdenkbb1b8262003-03-27 12:09:35 +000010#include <image.h>
Jean-Christophe PLAGNIOL-VILLARD6bb94492009-04-04 12:49:11 +020011#include <u-boot/zlib.h>
wdenkbb1b8262003-03-27 12:09:35 +000012#include <asm/byteorder.h>
13#include <asm/addrspace.h>
14
Wolfgang Denk6405a152006-03-31 18:32:53 +020015DECLARE_GLOBAL_DATA_PTR;
16
wdenkbb1b8262003-03-27 12:09:35 +000017#define LINUX_MAX_ENVS 256
18#define LINUX_MAX_ARGS 256
19
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +020020#if defined(CONFIG_QEMU_MALTA)
21#define mips_boot_qemu_malta 1
22#else
23#define mips_boot_qemu_malta 0
24#endif
25
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020026static int linux_argc;
27static char **linux_argv;
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +020028static char *linux_argp;
wdenkbb1b8262003-03-27 12:09:35 +000029
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +020030static char **linux_env;
31static char *linux_env_p;
32static int linux_env_idx;
wdenkbb1b8262003-03-27 12:09:35 +000033
Daniel Schwierzeck22d1c232013-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 Schwierzeckeb782d62013-05-09 17:10:06 +020055static void linux_cmdline_init(void)
56{
57 linux_argc = 1;
58 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
59 linux_argv[0] = 0;
60 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
61}
62
63static void linux_cmdline_set(const char *value, size_t len)
64{
65 linux_argv[linux_argc] = linux_argp;
66 memcpy(linux_argp, value, len);
67 linux_argp[len] = 0;
68
69 linux_argp += len + 1;
70 linux_argc++;
71}
72
73static void linux_cmdline_dump(void)
74{
75 int i;
76
77 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
78 linux_argv, linux_argp);
79
80 for (i = 1; i < linux_argc; i++)
81 debug(" arg %03d: %s\n", i, linux_argv[i]);
82}
83
84static void boot_cmdline_linux(bootm_headers_t *images)
85{
86 const char *bootargs, *next, *quote;
87
88 linux_cmdline_init();
89
90 bootargs = getenv("bootargs");
91 if (!bootargs)
92 return;
93
94 next = bootargs;
95
96 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
97 quote = strchr(bootargs, '"');
98 next = strchr(bootargs, ' ');
99
100 while (next && quote && quote < next) {
101 /*
102 * we found a left quote before the next blank
103 * now we have to find the matching right quote
104 */
105 next = strchr(quote + 1, '"');
106 if (next) {
107 quote = strchr(next + 1, '"');
108 next = strchr(next + 1, ' ');
109 }
110 }
111
112 if (!next)
113 next = bootargs + strlen(bootargs);
114
115 linux_cmdline_set(bootargs, next - bootargs);
116
117 if (*next)
118 next++;
119
120 bootargs = next;
121 }
122
123 linux_cmdline_dump();
124}
125
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200126static void linux_env_init(void)
127{
128 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
129 linux_env[0] = 0;
130 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
131 linux_env_idx = 0;
132}
133
134static void linux_env_set(const char *env_name, const char *env_val)
135{
136 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
137 linux_env[linux_env_idx] = linux_env_p;
138
139 strcpy(linux_env_p, env_name);
140 linux_env_p += strlen(env_name);
141
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200142 if (mips_boot_qemu_malta) {
143 linux_env_p++;
144 linux_env[++linux_env_idx] = linux_env_p;
145 } else {
146 *linux_env_p++ = '=';
147 }
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200148
149 strcpy(linux_env_p, env_val);
150 linux_env_p += strlen(env_val);
151
152 linux_env_p++;
153 linux_env[++linux_env_idx] = 0;
154 }
155}
156
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000157static void boot_prep_linux(bootm_headers_t *images)
wdenkbb1b8262003-03-27 12:09:35 +0000158{
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200159 char env_buf[12];
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200160 const char *cp;
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200161 ulong rd_start, rd_size;
wdenkbb1b8262003-03-27 12:09:35 +0000162
wdenk9b7f3842003-10-09 20:09:04 +0000163#ifdef CONFIG_MEMSIZE_IN_BYTES
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200164 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
165 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
wdenk9b7f3842003-10-09 20:09:04 +0000166#else
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200167 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
168 debug("## Giving linux memsize in MB, %lu\n",
Daniel Schwierzeck40f30c02013-05-09 17:10:06 +0200169 (ulong)(gd->ram_size >> 20));
wdenk9b7f3842003-10-09 20:09:04 +0000170#endif /* CONFIG_MEMSIZE_IN_BYTES */
wdenkbb1b8262003-03-27 12:09:35 +0000171
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200172 rd_start = UNCACHED_SDRAM(images->initrd_start);
173 rd_size = images->initrd_end - images->initrd_start;
174
Daniel Schwierzeck46145fb2013-05-09 17:10:06 +0200175 linux_env_init();
176
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200177 linux_env_set("memsize", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000178
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200179 sprintf(env_buf, "0x%08lX", rd_start);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200180 linux_env_set("initrd_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000181
Daniel Schwierzeckeaadb1a2013-05-09 17:10:06 +0200182 sprintf(env_buf, "0x%lX", rd_size);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200183 linux_env_set("initrd_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000184
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200185 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
186 linux_env_set("flash_start", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000187
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200188 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
189 linux_env_set("flash_size", env_buf);
wdenkbb1b8262003-03-27 12:09:35 +0000190
Jason McMullancfbe4572008-06-08 23:56:00 -0400191 cp = getenv("ethaddr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200192 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400193 linux_env_set("ethaddr", cp);
Jason McMullancfbe4572008-06-08 23:56:00 -0400194
195 cp = getenv("eth1addr");
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200196 if (cp)
Jason McMullancfbe4572008-06-08 23:56:00 -0400197 linux_env_set("eth1addr", cp);
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200198
199 if (mips_boot_qemu_malta)
200 linux_env_set("modetty0", "38400n8r");
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000201}
202
203static void boot_jump_linux(bootm_headers_t *images)
204{
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200205 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
206 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200207 ulong linux_extra = 0;
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000208
Daniel Schwierzeck0b95a7c2013-05-09 17:10:06 +0200209 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000210
211 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
212
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200213 if (mips_boot_qemu_malta)
214 linux_extra = gd->ram_size;
215
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000216 /* we assume that the kernel is in place */
217 printf("\nStarting kernel ...\n\n");
218
Daniel Schwierzeck0dfa0b32013-05-30 19:04:20 +0200219 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000220}
221
222int do_bootm_linux(int flag, int argc, char * const argv[],
223 bootm_headers_t *images)
224{
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000225 /* No need for those on MIPS */
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200226 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000227 return -1;
228
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200229 if (flag & BOOTM_STATE_OS_CMDLINE) {
230 boot_cmdline_linux(images);
231 return 0;
232 }
233
Gabor Juhos7f959fd2013-01-07 02:53:42 +0000234 if (flag & BOOTM_STATE_OS_PREP) {
235 boot_prep_linux(images);
236 return 0;
237 }
238
239 if (flag & BOOTM_STATE_OS_GO) {
240 boot_jump_linux(images);
241 return 0;
242 }
Jason McMullancfbe4572008-06-08 23:56:00 -0400243
Daniel Schwierzeckeb782d62013-05-09 17:10:06 +0200244 boot_cmdline_linux(images);
Gabor Juhos72cb5f02013-01-07 02:53:41 +0000245 boot_prep_linux(images);
Gabor Juhose0d783b2013-01-07 02:53:40 +0000246 boot_jump_linux(images);
Daniel Schwierzeck83010eb2012-06-03 23:46:04 +0200247
Marian Balakowiczdf8ff332008-03-12 10:33:00 +0100248 /* does not return */
Kumar Gala48626aa2008-08-15 08:24:45 -0500249 return 1;
wdenkbb1b8262003-03-27 12:09:35 +0000250}