blob: 9dc47407684bd64973730c02d8a6ed7f5674453c [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>
Daniel Schwierzeck5002d8c2015-01-14 21:44:13 +010010#include <fdt_support.h>
wdenkc0218802003-03-27 12:09:35 +000011#include <asm/addrspace.h>
Purna Chandra Mandalfdff5b02016-04-18 18:31:38 +053012#include <asm/io.h>
wdenkc0218802003-03-27 12:09:35 +000013
Wolfgang Denkd87080b2006-03-31 18:32:53 +020014DECLARE_GLOBAL_DATA_PTR;
15
wdenkc0218802003-03-27 12:09:35 +000016#define LINUX_MAX_ENVS 256
17#define LINUX_MAX_ARGS 256
18
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020019static int linux_argc;
20static char **linux_argv;
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020021static char *linux_argp;
wdenkc0218802003-03-27 12:09:35 +000022
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +020023static char **linux_env;
24static char *linux_env_p;
25static int linux_env_idx;
wdenkc0218802003-03-27 12:09:35 +000026
Daniel Schwierzeckf66cc1e2013-05-09 17:10:06 +020027static ulong arch_get_sp(void)
28{
29 ulong ret;
30
31 __asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
32
33 return ret;
34}
35
36void arch_lmb_reserve(struct lmb *lmb)
37{
38 ulong sp;
39
40 sp = arch_get_sp();
41 debug("## Current stack ends at 0x%08lx\n", sp);
42
43 /* adjust sp by 4K to be safe */
44 sp -= 4096;
Paul Burton7a3e0f72016-09-26 19:28:56 +010045 lmb_reserve(lmb, sp, gd->ram_top - sp);
Daniel Schwierzeckf66cc1e2013-05-09 17:10:06 +020046}
47
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020048static void linux_cmdline_init(void)
49{
50 linux_argc = 1;
51 linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
52 linux_argv[0] = 0;
53 linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
54}
55
56static void linux_cmdline_set(const char *value, size_t len)
57{
58 linux_argv[linux_argc] = linux_argp;
59 memcpy(linux_argp, value, len);
60 linux_argp[len] = 0;
61
62 linux_argp += len + 1;
63 linux_argc++;
64}
65
66static void linux_cmdline_dump(void)
67{
68 int i;
69
70 debug("## cmdline argv at 0x%p, argp at 0x%p\n",
71 linux_argv, linux_argp);
72
73 for (i = 1; i < linux_argc; i++)
74 debug(" arg %03d: %s\n", i, linux_argv[i]);
75}
76
Daniel Schwierzeck25fc6642015-01-14 21:44:13 +010077static void linux_cmdline_legacy(bootm_headers_t *images)
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020078{
79 const char *bootargs, *next, *quote;
80
81 linux_cmdline_init();
82
Simon Glass00caae62017-08-03 12:22:12 -060083 bootargs = env_get("bootargs");
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +020084 if (!bootargs)
85 return;
86
87 next = bootargs;
88
89 while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
90 quote = strchr(bootargs, '"');
91 next = strchr(bootargs, ' ');
92
93 while (next && quote && quote < next) {
94 /*
95 * we found a left quote before the next blank
96 * now we have to find the matching right quote
97 */
98 next = strchr(quote + 1, '"');
99 if (next) {
100 quote = strchr(next + 1, '"');
101 next = strchr(next + 1, ' ');
102 }
103 }
104
105 if (!next)
106 next = bootargs + strlen(bootargs);
107
108 linux_cmdline_set(bootargs, next - bootargs);
109
110 if (*next)
111 next++;
112
113 bootargs = next;
114 }
Daniel Schwierzeck25fc6642015-01-14 21:44:13 +0100115}
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200116
Daniel Schwierzeck8cec7252015-01-14 21:44:13 +0100117static void linux_cmdline_append(bootm_headers_t *images)
118{
119 char buf[24];
120 ulong mem, rd_start, rd_size;
121
122 /* append mem */
123 mem = gd->ram_size >> 20;
124 sprintf(buf, "mem=%luM", mem);
125 linux_cmdline_set(buf, strlen(buf));
126
127 /* append rd_start and rd_size */
128 rd_start = images->initrd_start;
129 rd_size = images->initrd_end - images->initrd_start;
130
131 if (rd_size) {
132 sprintf(buf, "rd_start=0x%08lX", rd_start);
133 linux_cmdline_set(buf, strlen(buf));
134 sprintf(buf, "rd_size=0x%lX", rd_size);
135 linux_cmdline_set(buf, strlen(buf));
136 }
137}
138
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200139static void linux_env_init(void)
140{
141 linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
142 linux_env[0] = 0;
143 linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
144 linux_env_idx = 0;
145}
146
147static void linux_env_set(const char *env_name, const char *env_val)
148{
149 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
150 linux_env[linux_env_idx] = linux_env_p;
151
152 strcpy(linux_env_p, env_name);
153 linux_env_p += strlen(env_name);
154
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100155 if (CONFIG_IS_ENABLED(MALTA)) {
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200156 linux_env_p++;
157 linux_env[++linux_env_idx] = linux_env_p;
158 } else {
159 *linux_env_p++ = '=';
160 }
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200161
162 strcpy(linux_env_p, env_val);
163 linux_env_p += strlen(env_val);
164
165 linux_env_p++;
166 linux_env[++linux_env_idx] = 0;
167 }
168}
169
Daniel Schwierzeckca65e582015-01-14 21:44:13 +0100170static void linux_env_legacy(bootm_headers_t *images)
wdenkc0218802003-03-27 12:09:35 +0000171{
Daniel Schwierzecke51a6b72012-06-03 23:46:04 +0200172 char env_buf[12];
Daniel Schwierzeck15f8aa92013-05-09 17:10:06 +0200173 const char *cp;
Daniel Schwierzeck6c154552013-05-09 17:10:06 +0200174 ulong rd_start, rd_size;
wdenkc0218802003-03-27 12:09:35 +0000175
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100176 if (CONFIG_IS_ENABLED(MEMSIZE_IN_BYTES)) {
177 sprintf(env_buf, "%lu", (ulong)gd->ram_size);
178 debug("## Giving linux memsize in bytes, %lu\n",
179 (ulong)gd->ram_size);
180 } else {
181 sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
182 debug("## Giving linux memsize in MB, %lu\n",
183 (ulong)(gd->ram_size >> 20));
184 }
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
Simon Glass00caae62017-08-03 12:22:12 -0600205 cp = env_get("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
Simon Glass00caae62017-08-03 12:22:12 -0600209 cp = env_get("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
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100213 if (CONFIG_IS_ENABLED(MALTA)) {
Paul Burtond18d49d2013-11-26 17:45:25 +0000214 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
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100219static int boot_reloc_ramdisk(bootm_headers_t *images)
220{
221 ulong rd_len = images->rd_end - images->rd_start;
222
223 /*
224 * In case of legacy uImage's, relocation of ramdisk is already done
225 * by do_bootm_states() and should not repeated in 'bootm prep'.
226 */
227 if (images->state & BOOTM_STATE_RAMDISK) {
228 debug("## Ramdisk already relocated\n");
229 return 0;
230 }
231
232 return boot_ramdisk_high(&images->lmb, images->rd_start,
233 rd_len, &images->initrd_start, &images->initrd_end);
234}
235
236static int boot_reloc_fdt(bootm_headers_t *images)
237{
238 /*
239 * In case of legacy uImage's, relocation of FDT is already done
240 * by do_bootm_states() and should not repeated in 'bootm prep'.
241 */
242 if (images->state & BOOTM_STATE_FDT) {
243 debug("## FDT already relocated\n");
244 return 0;
245 }
246
247#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
248 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
249 return boot_relocate_fdt(&images->lmb, &images->ft_addr,
250 &images->ft_len);
251#else
252 return 0;
253#endif
254}
255
Alexey Brodkin42803422018-01-24 20:47:09 +0300256#if CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && CONFIG_IS_ENABLED(OF_LIBFDT)
Purna Chandra Mandalfdff5b02016-04-18 18:31:38 +0530257int arch_fixup_fdt(void *blob)
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100258{
Purna Chandra Mandalfdff5b02016-04-18 18:31:38 +0530259 u64 mem_start = virt_to_phys((void *)gd->bd->bi_memstart);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100260 u64 mem_size = gd->ram_size;
261
262 return fdt_fixup_memory_banks(blob, &mem_start, &mem_size, 1);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100263}
Alexey Brodkin42803422018-01-24 20:47:09 +0300264#endif
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100265
266static int boot_setup_fdt(bootm_headers_t *images)
267{
268 return image_setup_libfdt(images, images->ft_addr, images->ft_len,
269 &images->lmb);
270}
271
Daniel Schwierzeckca65e582015-01-14 21:44:13 +0100272static void boot_prep_linux(bootm_headers_t *images)
273{
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100274 boot_reloc_ramdisk(images);
Daniel Schwierzeck5002d8c2015-01-14 21:44:13 +0100275
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100276 if (CONFIG_IS_ENABLED(MIPS_BOOT_FDT) && images->ft_len) {
277 boot_reloc_fdt(images);
Daniel Schwierzeck5002d8c2015-01-14 21:44:13 +0100278 boot_setup_fdt(images);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100279 } else {
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100280 if (CONFIG_IS_ENABLED(MIPS_BOOT_CMDLINE_LEGACY)) {
281 linux_cmdline_legacy(images);
282
Zubair Lutfullah Kakakhel48bfc312017-07-11 16:47:51 +0100283 if (!CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100284 linux_cmdline_append(images);
285
286 linux_cmdline_dump();
287 }
Zubair Lutfullah Kakakhel48bfc312017-07-11 16:47:51 +0100288
289 if (CONFIG_IS_ENABLED(MIPS_BOOT_ENV_LEGACY))
290 linux_env_legacy(images);
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100291 }
Daniel Schwierzeckca65e582015-01-14 21:44:13 +0100292}
293
Gabor Juhos0ea72132013-01-07 02:53:41 +0000294static void boot_jump_linux(bootm_headers_t *images)
295{
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200296 typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
297 kernel_entry_t kernel = (kernel_entry_t) images->ep;
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200298 ulong linux_extra = 0;
Gabor Juhos0ea72132013-01-07 02:53:41 +0000299
Daniel Schwierzeckc4b37842013-05-09 17:10:06 +0200300 debug("## Transferring control to Linux (at address %p) ...\n", kernel);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000301
302 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
303
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100304 if (CONFIG_IS_ENABLED(MALTA))
Daniel Schwierzeckb87493f2013-05-30 19:04:20 +0200305 linux_extra = gd->ram_size;
306
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100307#if CONFIG_IS_ENABLED(BOOTSTAGE_FDT)
Daniel Schwierzecke13a50b2015-01-14 21:44:13 +0100308 bootstage_fdt_add_report();
309#endif
Daniel Schwierzeck347ea942015-11-01 17:36:15 +0100310#if CONFIG_IS_ENABLED(BOOTSTAGE_REPORT)
Daniel Schwierzecke13a50b2015-01-14 21:44:13 +0100311 bootstage_report();
312#endif
Gabor Juhos0ea72132013-01-07 02:53:41 +0000313
Daniel Schwierzeck90b1c9f2015-02-22 16:58:30 +0100314 if (images->ft_len)
315 kernel(-2, (ulong)images->ft_addr, 0, 0);
316 else
317 kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env,
318 linux_extra);
Gabor Juhos0ea72132013-01-07 02:53:41 +0000319}
320
321int do_bootm_linux(int flag, int argc, char * const argv[],
322 bootm_headers_t *images)
323{
Gabor Juhos9c170e22013-01-07 02:53:42 +0000324 /* No need for those on MIPS */
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200325 if (flag & BOOTM_STATE_OS_BD_T)
Gabor Juhos9c170e22013-01-07 02:53:42 +0000326 return -1;
327
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100328 /*
329 * Cmdline init has been moved to 'bootm prep' because it has to be
330 * done after relocation of ramdisk to always pass correct values
331 * for rd_start and rd_size to Linux kernel.
332 */
333 if (flag & BOOTM_STATE_OS_CMDLINE)
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200334 return 0;
Daniel Schwierzeck59e8cbd2013-05-09 17:10:06 +0200335
Gabor Juhos9c170e22013-01-07 02:53:42 +0000336 if (flag & BOOTM_STATE_OS_PREP) {
337 boot_prep_linux(images);
338 return 0;
339 }
340
Daniel Schwierzeck2bb5b632015-11-01 17:36:14 +0100341 if (flag & (BOOTM_STATE_OS_GO | BOOTM_STATE_OS_FAKE_GO)) {
Gabor Juhos9c170e22013-01-07 02:53:42 +0000342 boot_jump_linux(images);
343 return 0;
344 }
Gabor Juhos0ea72132013-01-07 02:53:41 +0000345
Marian Balakowiczcd7c5962008-03-12 10:33:00 +0100346 /* does not return */
Kumar Gala40d7e992008-08-15 08:24:45 -0500347 return 1;
wdenkc0218802003-03-27 12:09:35 +0000348}