blob: ba9fd687eccad302861b8168ca3777269e22442d [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
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020020static int linux_argc;
21static char **linux_argv;
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020022static char *linux_argp;
wdenkc0218802003-03-27 12:09:35 +000023
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020024static char **linux_env;
25static char *linux_env_p;
26static int linux_env_idx;
wdenkc0218802003-03-27 12:09:35 +000027
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020028static void linux_params_init(void);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020029static void linux_env_set(char *env_name, char *env_val);
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 Schwierzeck59e8cbd2013-05-09 17:10:06 +020052static void linux_cmdline_init(void)
53{
54 linux_argc = 1;
55 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
56 linux_argv[0] = 0;
57 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
58}
59
60static void linux_cmdline_set(const char *value, size_t len)
61{
62 linux_argv[linux_argc] = linux_argp;
63 memcpy(linux_argp, value, len);
64 linux_argp[len] = 0;
65
66 linux_argp += len + 1;
67 linux_argc++;
68}
69
70static void linux_cmdline_dump(void)
71{
72 int i;
73
74 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
75 linux_argv, linux_argp);
76
77 for (i = 1; i < linux_argc; i++)
78 debug(" arg %03d: %s\n", i, linux_argv[i]);
79}
80
81static void boot_cmdline_linux(bootm_headers_t *images)
82{
83 const char *bootargs, *next, *quote;
84
85 linux_cmdline_init();
86
87 bootargs = getenv("bootargs");
88 if (!bootargs)
89 return;
90
91 next = bootargs;
92
93 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
94 quote = strchr(bootargs, '"');
95 next = strchr(bootargs, ' ');
96
97 while (next && quote && quote < next) {
98 /*
99 * we found a left quote before the next blank
100 * now we have to find the matching right quote
101 */
102 next = strchr(quote + 1, '"');
103 if (next) {
104 quote = strchr(next + 1, '"');
105 next = strchr(next + 1, ' ');
106 }
107 }
108
109 if (!next)
110 next = bootargs + strlen(bootargs);
111
112 linux_cmdline_set(bootargs, next - bootargs);
113
114 if (*next)
115 next++;
116
117 bootargs = next;
118 }
119
120 linux_cmdline_dump();
121}
122
Gabor Juhos0ea72132013-01-07 02:53:41 +0000123static void boot_prep_linux(bootm_headers_t *images)
wdenkc0218802003-03-27 12:09:35 +0000124{
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200125 char env_buf[12];
126 char *cp;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100127
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200128 linux_params_init();
wdenkc0218802003-03-27 12:09:35 +0000129
wdenk5da627a2003-10-09 20:09:04 +0000130#ifdef CONFIG_MEMSIZE_IN_BYTES
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200131 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
132 debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
wdenk5da627a2003-10-09 20:09:04 +0000133#else
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200134 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
135 debug("## Giving linux memsize in MB, %lu\n",
Daniel Schwierzeck45bde482013-05-09 17:10:06 +0200136 (ulong)(gd->ram_size >> 20));
wdenk5da627a2003-10-09 20:09:04 +0000137#endif /* CONFIG_MEMSIZE_IN_BYTES */
wdenkc0218802003-03-27 12:09:35 +0000138
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200139 linux_env_set("memsize", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000140
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200141 sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start));
142 linux_env_set("initrd_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000143
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200144 sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start));
145 linux_env_set("initrd_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000146
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200147 sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
148 linux_env_set("flash_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000149
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200150 sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
151 linux_env_set("flash_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000152
Jason McMullane7c37452008-06-08 23:56:00 -0400153 cp = getenv("ethaddr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200154 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400155 linux_env_set("ethaddr", cp);
Jason McMullane7c37452008-06-08 23:56:00 -0400156
157 cp = getenv("eth1addr");
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200158 if (cp)
Jason McMullane7c37452008-06-08 23:56:00 -0400159 linux_env_set("eth1addr", cp);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000160}
Jason McMullane7c37452008-06-08 23:56:00 -0400161
Gabor Juhos0ea72132013-01-07 02:53:41 +0000162static void boot_jump_linux(bootm_headers_t *images)
163{
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200164 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
165 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Gabor Juhos0ea72132013-01-07 02:53:41 +0000166
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200167 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000168
169 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
170
171 /* we assume that the kernel is in place */
172 printf("\nStarting kernel ...\n\n");
173
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200174 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, 0);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000175}
176
177int do_bootm_linux(int flag, int argc, char * const argv[],
178 bootm_headers_t *images)
179{
Gabor Juhos9c170e22013-01-07 02:53:42 +0000180 /* No need for those on MIPS */
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200181 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos9c170e22013-01-07 02:53:42 +0000182 return -1;
183
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200184 if (flag & BOOTM_STATE_OS_CMDLINE) {
185 boot_cmdline_linux(images);
186 return 0;
187 }
188
Gabor Juhos9c170e22013-01-07 02:53:42 +0000189 if (flag & BOOTM_STATE_OS_PREP) {
190 boot_prep_linux(images);
191 return 0;
192 }
193
194 if (flag & BOOTM_STATE_OS_GO) {
195 boot_jump_linux(images);
196 return 0;
197 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000198
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200199 boot_cmdline_linux(images);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000200 boot_prep_linux(images);
Gabor Juhose08634c2013-01-07 02:53:40 +0000201 boot_jump_linux(images);
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200202
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100203 /* does not return */
Kumar Gala40d7e992008-08-15 08:24:45 -0500204 return 1;
wdenkc0218802003-03-27 12:09:35 +0000205}
206
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200207static void linux_params_init(void)
wdenkc0218802003-03-27 12:09:35 +0000208{
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200209 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
wdenk5da627a2003-10-09 20:09:04 +0000210 linux_env[0] = 0;
Daniel Schwierzeck45bde482013-05-09 17:10:06 +0200211 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
wdenk5da627a2003-10-09 20:09:04 +0000212 linux_env_idx = 0;
wdenkc0218802003-03-27 12:09:35 +0000213}
214
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200215static void linux_env_set(char *env_name, char *env_val)
wdenkc0218802003-03-27 12:09:35 +0000216{
wdenk5da627a2003-10-09 20:09:04 +0000217 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
218 linux_env[linux_env_idx] = linux_env_p;
wdenkc0218802003-03-27 12:09:35 +0000219
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200220 strcpy(linux_env_p, env_name);
221 linux_env_p += strlen(env_name);
wdenkc0218802003-03-27 12:09:35 +0000222
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200223 strcpy(linux_env_p, "=");
wdenk5da627a2003-10-09 20:09:04 +0000224 linux_env_p += 1;
wdenkc0218802003-03-27 12:09:35 +0000225
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200226 strcpy(linux_env_p, env_val);
227 linux_env_p += strlen(env_val);
wdenk8bde7f72003-06-27 21:31:46 +0000228
wdenk5da627a2003-10-09 20:09:04 +0000229 linux_env_p++;
230 linux_env[++linux_env_idx] = 0;
231 }
wdenkc0218802003-03-27 12:09:35 +0000232}