]> git.sur5r.net Git - u-boot/blob - arch/mips/lib/bootm.c
MIPS: bootm: refactor preparation of Linux kernel environment
[u-boot] / arch / mips / lib / bootm.c
1 /*
2  * (C) Copyright 2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9 #include <image.h>
10 #include <asm/addrspace.h>
11
12 DECLARE_GLOBAL_DATA_PTR;
13
14 #define LINUX_MAX_ENVS          256
15 #define LINUX_MAX_ARGS          256
16
17 #if defined(CONFIG_MALTA)
18 #define mips_boot_malta         1
19 #else
20 #define mips_boot_malta         0
21 #endif
22
23 #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
29 #if defined(CONFIG_MIPS_BOOT_ENV_LEGACY)
30 #define mips_boot_env_legacy    1
31 #else
32 #define mips_boot_env_legacy    0
33 #endif
34
35 static int linux_argc;
36 static char **linux_argv;
37 static char *linux_argp;
38
39 static char **linux_env;
40 static char *linux_env_p;
41 static int linux_env_idx;
42
43 static ulong arch_get_sp(void)
44 {
45         ulong ret;
46
47         __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
48
49         return ret;
50 }
51
52 void arch_lmb_reserve(struct lmb *lmb)
53 {
54         ulong sp;
55
56         sp = arch_get_sp();
57         debug("## Current stack ends at 0x%08lx\n", sp);
58
59         /* adjust sp by 4K to be safe */
60         sp -= 4096;
61         lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
62 }
63
64 static int boot_setup_linux(bootm_headers_t *images)
65 {
66         int ret;
67         ulong rd_len;
68
69         rd_len = images->rd_end - images->rd_start;
70         ret = boot_ramdisk_high(&images->lmb, images->rd_start,
71                 rd_len, &images->initrd_start, &images->initrd_end);
72         if (ret)
73                 return ret;
74
75         return 0;
76 }
77
78 static void linux_cmdline_init(void)
79 {
80         linux_argc = 1;
81         linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
82         linux_argv[0] = 0;
83         linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
84 }
85
86 static void linux_cmdline_set(const char *value, size_t len)
87 {
88         linux_argv[linux_argc] = linux_argp;
89         memcpy(linux_argp, value, len);
90         linux_argp[len] = 0;
91
92         linux_argp += len + 1;
93         linux_argc++;
94 }
95
96 static void linux_cmdline_dump(void)
97 {
98         int i;
99
100         debug("## cmdline argv at 0x%p, argp at 0x%p\n",
101               linux_argv, linux_argp);
102
103         for (i = 1; i < linux_argc; i++)
104                 debug("   arg %03d: %s\n", i, linux_argv[i]);
105 }
106
107 static void linux_cmdline_legacy(bootm_headers_t *images)
108 {
109         const char *bootargs, *next, *quote;
110
111         linux_cmdline_init();
112
113         bootargs = getenv("bootargs");
114         if (!bootargs)
115                 return;
116
117         next = bootargs;
118
119         while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
120                 quote = strchr(bootargs, '"');
121                 next = strchr(bootargs, ' ');
122
123                 while (next && quote && quote < next) {
124                         /*
125                          * we found a left quote before the next blank
126                          * now we have to find the matching right quote
127                          */
128                         next = strchr(quote + 1, '"');
129                         if (next) {
130                                 quote = strchr(next + 1, '"');
131                                 next = strchr(next + 1, ' ');
132                         }
133                 }
134
135                 if (!next)
136                         next = bootargs + strlen(bootargs);
137
138                 linux_cmdline_set(bootargs, next - bootargs);
139
140                 if (*next)
141                         next++;
142
143                 bootargs = next;
144         }
145 }
146
147 static void boot_cmdline_linux(bootm_headers_t *images)
148 {
149         if (mips_boot_cmdline_legacy) {
150                 linux_cmdline_legacy(images);
151                 linux_cmdline_dump();
152         }
153 }
154
155 static void linux_env_init(void)
156 {
157         linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
158         linux_env[0] = 0;
159         linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
160         linux_env_idx = 0;
161 }
162
163 static void linux_env_set(const char *env_name, const char *env_val)
164 {
165         if (linux_env_idx < LINUX_MAX_ENVS - 1) {
166                 linux_env[linux_env_idx] = linux_env_p;
167
168                 strcpy(linux_env_p, env_name);
169                 linux_env_p += strlen(env_name);
170
171                 if (mips_boot_malta) {
172                         linux_env_p++;
173                         linux_env[++linux_env_idx] = linux_env_p;
174                 } else {
175                         *linux_env_p++ = '=';
176                 }
177
178                 strcpy(linux_env_p, env_val);
179                 linux_env_p += strlen(env_val);
180
181                 linux_env_p++;
182                 linux_env[++linux_env_idx] = 0;
183         }
184 }
185
186 static void linux_env_legacy(bootm_headers_t *images)
187 {
188         char env_buf[12];
189         const char *cp;
190         ulong rd_start, rd_size;
191
192 #ifdef CONFIG_MEMSIZE_IN_BYTES
193         sprintf(env_buf, "%lu", (ulong)gd->ram_size);
194         debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
195 #else
196         sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
197         debug("## Giving linux memsize in MB, %lu\n",
198               (ulong)(gd->ram_size >> 20));
199 #endif /* CONFIG_MEMSIZE_IN_BYTES */
200
201         rd_start = UNCACHED_SDRAM(images->initrd_start);
202         rd_size = images->initrd_end - images->initrd_start;
203
204         linux_env_init();
205
206         linux_env_set("memsize", env_buf);
207
208         sprintf(env_buf, "0x%08lX", rd_start);
209         linux_env_set("initrd_start", env_buf);
210
211         sprintf(env_buf, "0x%lX", rd_size);
212         linux_env_set("initrd_size", env_buf);
213
214         sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
215         linux_env_set("flash_start", env_buf);
216
217         sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
218         linux_env_set("flash_size", env_buf);
219
220         cp = getenv("ethaddr");
221         if (cp)
222                 linux_env_set("ethaddr", cp);
223
224         cp = getenv("eth1addr");
225         if (cp)
226                 linux_env_set("eth1addr", cp);
227
228         if (mips_boot_malta) {
229                 sprintf(env_buf, "%un8r", gd->baudrate);
230                 linux_env_set("modetty0", env_buf);
231         }
232 }
233
234 static void boot_prep_linux(bootm_headers_t *images)
235 {
236         if (mips_boot_env_legacy)
237                 linux_env_legacy(images);
238 }
239
240 static void boot_jump_linux(bootm_headers_t *images)
241 {
242         typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
243         kernel_entry_t kernel = (kernel_entry_t) images->ep;
244         ulong linux_extra = 0;
245
246         debug("## Transferring control to Linux (at address %p) ...\n", kernel);
247
248         bootstage_mark(BOOTSTAGE_ID_RUN_OS);
249
250         if (mips_boot_malta)
251                 linux_extra = gd->ram_size;
252
253         /* we assume that the kernel is in place */
254         printf("\nStarting kernel ...\n\n");
255
256         kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
257 }
258
259 int do_bootm_linux(int flag, int argc, char * const argv[],
260                         bootm_headers_t *images)
261 {
262         int ret;
263
264         /* No need for those on MIPS */
265         if (flag & BOOTM_STATE_OS_BD_T)
266                 return -1;
267
268         if (flag & BOOTM_STATE_OS_CMDLINE) {
269                 boot_cmdline_linux(images);
270                 return 0;
271         }
272
273         if (flag & BOOTM_STATE_OS_PREP) {
274                 boot_prep_linux(images);
275                 return 0;
276         }
277
278         if (flag & BOOTM_STATE_OS_GO) {
279                 boot_jump_linux(images);
280                 return 0;
281         }
282
283         ret = boot_setup_linux(images);
284         if (ret)
285                 return ret;
286
287         boot_cmdline_linux(images);
288         boot_prep_linux(images);
289         boot_jump_linux(images);
290
291         /* does not return */
292         return 1;
293 }