xref: /rk3399_rockchip-uboot/arch/mips/lib/bootm.c (revision ca65e5851fb60ae58b46e2ad76a90b39d9c378c3)
1ea0364f1SPeter Tyser /*
2ea0364f1SPeter Tyser  * (C) Copyright 2003
3ea0364f1SPeter Tyser  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4ea0364f1SPeter Tyser  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
6ea0364f1SPeter Tyser  */
7ea0364f1SPeter Tyser 
8ea0364f1SPeter Tyser #include <common.h>
9ea0364f1SPeter Tyser #include <image.h>
10ea0364f1SPeter Tyser #include <asm/addrspace.h>
11ea0364f1SPeter Tyser 
12ea0364f1SPeter Tyser DECLARE_GLOBAL_DATA_PTR;
13ea0364f1SPeter Tyser 
14ea0364f1SPeter Tyser #define	LINUX_MAX_ENVS		256
15ea0364f1SPeter Tyser #define	LINUX_MAX_ARGS		256
16ea0364f1SPeter Tyser 
177a9d109bSPaul Burton #if defined(CONFIG_MALTA)
187a9d109bSPaul Burton #define mips_boot_malta		1
19b87493f4SDaniel Schwierzeck #else
207a9d109bSPaul Burton #define mips_boot_malta		0
21b87493f4SDaniel Schwierzeck #endif
22b87493f4SDaniel Schwierzeck 
2325fc664fSDaniel Schwierzeck #if defined(CONFIG_MIPS_BOOT_CMDLINE_LEGACY)
2425fc664fSDaniel Schwierzeck #define mips_boot_cmdline_legacy	1
2525fc664fSDaniel Schwierzeck #else
2625fc664fSDaniel Schwierzeck #define mips_boot_cmdline_legacy	0
2725fc664fSDaniel Schwierzeck #endif
2825fc664fSDaniel Schwierzeck 
29*ca65e585SDaniel Schwierzeck #if defined(CONFIG_MIPS_BOOT_ENV_LEGACY)
30*ca65e585SDaniel Schwierzeck #define mips_boot_env_legacy	1
31*ca65e585SDaniel Schwierzeck #else
32*ca65e585SDaniel Schwierzeck #define mips_boot_env_legacy	0
33*ca65e585SDaniel Schwierzeck #endif
34*ca65e585SDaniel Schwierzeck 
35ea0364f1SPeter Tyser static int linux_argc;
36ea0364f1SPeter Tyser static char **linux_argv;
3759e8cbdbSDaniel Schwierzeck static char *linux_argp;
38ea0364f1SPeter Tyser 
39ea0364f1SPeter Tyser static char **linux_env;
40ea0364f1SPeter Tyser static char *linux_env_p;
41ea0364f1SPeter Tyser static int linux_env_idx;
42ea0364f1SPeter Tyser 
43f66cc1e3SDaniel Schwierzeck static ulong arch_get_sp(void)
44f66cc1e3SDaniel Schwierzeck {
45f66cc1e3SDaniel Schwierzeck 	ulong ret;
46f66cc1e3SDaniel Schwierzeck 
47f66cc1e3SDaniel Schwierzeck 	__asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
48f66cc1e3SDaniel Schwierzeck 
49f66cc1e3SDaniel Schwierzeck 	return ret;
50f66cc1e3SDaniel Schwierzeck }
51f66cc1e3SDaniel Schwierzeck 
52f66cc1e3SDaniel Schwierzeck void arch_lmb_reserve(struct lmb *lmb)
53f66cc1e3SDaniel Schwierzeck {
54f66cc1e3SDaniel Schwierzeck 	ulong sp;
55f66cc1e3SDaniel Schwierzeck 
56f66cc1e3SDaniel Schwierzeck 	sp = arch_get_sp();
57f66cc1e3SDaniel Schwierzeck 	debug("## Current stack ends at 0x%08lx\n", sp);
58f66cc1e3SDaniel Schwierzeck 
59f66cc1e3SDaniel Schwierzeck 	/* adjust sp by 4K to be safe */
60f66cc1e3SDaniel Schwierzeck 	sp -= 4096;
61f66cc1e3SDaniel Schwierzeck 	lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
62f66cc1e3SDaniel Schwierzeck }
63f66cc1e3SDaniel Schwierzeck 
64c9639421SDaniel Schwierzeck static int boot_setup_linux(bootm_headers_t *images)
65c9639421SDaniel Schwierzeck {
66c9639421SDaniel Schwierzeck 	int ret;
67c9639421SDaniel Schwierzeck 	ulong rd_len;
68c9639421SDaniel Schwierzeck 
69c9639421SDaniel Schwierzeck 	rd_len = images->rd_end - images->rd_start;
70c9639421SDaniel Schwierzeck 	ret = boot_ramdisk_high(&images->lmb, images->rd_start,
71c9639421SDaniel Schwierzeck 		rd_len, &images->initrd_start, &images->initrd_end);
72c9639421SDaniel Schwierzeck 	if (ret)
73c9639421SDaniel Schwierzeck 		return ret;
74c9639421SDaniel Schwierzeck 
75c9639421SDaniel Schwierzeck 	return 0;
76c9639421SDaniel Schwierzeck }
77c9639421SDaniel Schwierzeck 
7859e8cbdbSDaniel Schwierzeck static void linux_cmdline_init(void)
7959e8cbdbSDaniel Schwierzeck {
8059e8cbdbSDaniel Schwierzeck 	linux_argc = 1;
8159e8cbdbSDaniel Schwierzeck 	linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
8259e8cbdbSDaniel Schwierzeck 	linux_argv[0] = 0;
8359e8cbdbSDaniel Schwierzeck 	linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
8459e8cbdbSDaniel Schwierzeck }
8559e8cbdbSDaniel Schwierzeck 
8659e8cbdbSDaniel Schwierzeck static void linux_cmdline_set(const char *value, size_t len)
8759e8cbdbSDaniel Schwierzeck {
8859e8cbdbSDaniel Schwierzeck 	linux_argv[linux_argc] = linux_argp;
8959e8cbdbSDaniel Schwierzeck 	memcpy(linux_argp, value, len);
9059e8cbdbSDaniel Schwierzeck 	linux_argp[len] = 0;
9159e8cbdbSDaniel Schwierzeck 
9259e8cbdbSDaniel Schwierzeck 	linux_argp += len + 1;
9359e8cbdbSDaniel Schwierzeck 	linux_argc++;
9459e8cbdbSDaniel Schwierzeck }
9559e8cbdbSDaniel Schwierzeck 
9659e8cbdbSDaniel Schwierzeck static void linux_cmdline_dump(void)
9759e8cbdbSDaniel Schwierzeck {
9859e8cbdbSDaniel Schwierzeck 	int i;
9959e8cbdbSDaniel Schwierzeck 
10059e8cbdbSDaniel Schwierzeck 	debug("## cmdline argv at 0x%p, argp at 0x%p\n",
10159e8cbdbSDaniel Schwierzeck 	      linux_argv, linux_argp);
10259e8cbdbSDaniel Schwierzeck 
10359e8cbdbSDaniel Schwierzeck 	for (i = 1; i < linux_argc; i++)
10459e8cbdbSDaniel Schwierzeck 		debug("   arg %03d: %s\n", i, linux_argv[i]);
10559e8cbdbSDaniel Schwierzeck }
10659e8cbdbSDaniel Schwierzeck 
10725fc664fSDaniel Schwierzeck static void linux_cmdline_legacy(bootm_headers_t *images)
10859e8cbdbSDaniel Schwierzeck {
10959e8cbdbSDaniel Schwierzeck 	const char *bootargs, *next, *quote;
11059e8cbdbSDaniel Schwierzeck 
11159e8cbdbSDaniel Schwierzeck 	linux_cmdline_init();
11259e8cbdbSDaniel Schwierzeck 
11359e8cbdbSDaniel Schwierzeck 	bootargs = getenv("bootargs");
11459e8cbdbSDaniel Schwierzeck 	if (!bootargs)
11559e8cbdbSDaniel Schwierzeck 		return;
11659e8cbdbSDaniel Schwierzeck 
11759e8cbdbSDaniel Schwierzeck 	next = bootargs;
11859e8cbdbSDaniel Schwierzeck 
11959e8cbdbSDaniel Schwierzeck 	while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
12059e8cbdbSDaniel Schwierzeck 		quote = strchr(bootargs, '"');
12159e8cbdbSDaniel Schwierzeck 		next = strchr(bootargs, ' ');
12259e8cbdbSDaniel Schwierzeck 
12359e8cbdbSDaniel Schwierzeck 		while (next && quote && quote < next) {
12459e8cbdbSDaniel Schwierzeck 			/*
12559e8cbdbSDaniel Schwierzeck 			 * we found a left quote before the next blank
12659e8cbdbSDaniel Schwierzeck 			 * now we have to find the matching right quote
12759e8cbdbSDaniel Schwierzeck 			 */
12859e8cbdbSDaniel Schwierzeck 			next = strchr(quote + 1, '"');
12959e8cbdbSDaniel Schwierzeck 			if (next) {
13059e8cbdbSDaniel Schwierzeck 				quote = strchr(next + 1, '"');
13159e8cbdbSDaniel Schwierzeck 				next = strchr(next + 1, ' ');
13259e8cbdbSDaniel Schwierzeck 			}
13359e8cbdbSDaniel Schwierzeck 		}
13459e8cbdbSDaniel Schwierzeck 
13559e8cbdbSDaniel Schwierzeck 		if (!next)
13659e8cbdbSDaniel Schwierzeck 			next = bootargs + strlen(bootargs);
13759e8cbdbSDaniel Schwierzeck 
13859e8cbdbSDaniel Schwierzeck 		linux_cmdline_set(bootargs, next - bootargs);
13959e8cbdbSDaniel Schwierzeck 
14059e8cbdbSDaniel Schwierzeck 		if (*next)
14159e8cbdbSDaniel Schwierzeck 			next++;
14259e8cbdbSDaniel Schwierzeck 
14359e8cbdbSDaniel Schwierzeck 		bootargs = next;
14459e8cbdbSDaniel Schwierzeck 	}
14525fc664fSDaniel Schwierzeck }
14659e8cbdbSDaniel Schwierzeck 
14725fc664fSDaniel Schwierzeck static void boot_cmdline_linux(bootm_headers_t *images)
14825fc664fSDaniel Schwierzeck {
14925fc664fSDaniel Schwierzeck 	if (mips_boot_cmdline_legacy) {
15025fc664fSDaniel Schwierzeck 		linux_cmdline_legacy(images);
15159e8cbdbSDaniel Schwierzeck 		linux_cmdline_dump();
15259e8cbdbSDaniel Schwierzeck 	}
15325fc664fSDaniel Schwierzeck }
15459e8cbdbSDaniel Schwierzeck 
15515f8aa90SDaniel Schwierzeck static void linux_env_init(void)
15615f8aa90SDaniel Schwierzeck {
15715f8aa90SDaniel Schwierzeck 	linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
15815f8aa90SDaniel Schwierzeck 	linux_env[0] = 0;
15915f8aa90SDaniel Schwierzeck 	linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
16015f8aa90SDaniel Schwierzeck 	linux_env_idx = 0;
16115f8aa90SDaniel Schwierzeck }
16215f8aa90SDaniel Schwierzeck 
16315f8aa90SDaniel Schwierzeck static void linux_env_set(const char *env_name, const char *env_val)
16415f8aa90SDaniel Schwierzeck {
16515f8aa90SDaniel Schwierzeck 	if (linux_env_idx < LINUX_MAX_ENVS - 1) {
16615f8aa90SDaniel Schwierzeck 		linux_env[linux_env_idx] = linux_env_p;
16715f8aa90SDaniel Schwierzeck 
16815f8aa90SDaniel Schwierzeck 		strcpy(linux_env_p, env_name);
16915f8aa90SDaniel Schwierzeck 		linux_env_p += strlen(env_name);
17015f8aa90SDaniel Schwierzeck 
1717a9d109bSPaul Burton 		if (mips_boot_malta) {
172b87493f4SDaniel Schwierzeck 			linux_env_p++;
173b87493f4SDaniel Schwierzeck 			linux_env[++linux_env_idx] = linux_env_p;
174b87493f4SDaniel Schwierzeck 		} else {
17515f8aa90SDaniel Schwierzeck 			*linux_env_p++ = '=';
176b87493f4SDaniel Schwierzeck 		}
17715f8aa90SDaniel Schwierzeck 
17815f8aa90SDaniel Schwierzeck 		strcpy(linux_env_p, env_val);
17915f8aa90SDaniel Schwierzeck 		linux_env_p += strlen(env_val);
18015f8aa90SDaniel Schwierzeck 
18115f8aa90SDaniel Schwierzeck 		linux_env_p++;
18215f8aa90SDaniel Schwierzeck 		linux_env[++linux_env_idx] = 0;
18315f8aa90SDaniel Schwierzeck 	}
18415f8aa90SDaniel Schwierzeck }
18515f8aa90SDaniel Schwierzeck 
186*ca65e585SDaniel Schwierzeck static void linux_env_legacy(bootm_headers_t *images)
187ea0364f1SPeter Tyser {
188ea0364f1SPeter Tyser 	char env_buf[12];
18915f8aa90SDaniel Schwierzeck 	const char *cp;
1906c154552SDaniel Schwierzeck 	ulong rd_start, rd_size;
191ea0364f1SPeter Tyser 
192ea0364f1SPeter Tyser #ifdef CONFIG_MEMSIZE_IN_BYTES
193ea0364f1SPeter Tyser 	sprintf(env_buf, "%lu", (ulong)gd->ram_size);
194ea0364f1SPeter Tyser 	debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
195ea0364f1SPeter Tyser #else
196ea0364f1SPeter Tyser 	sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
197e51a6b7aSDaniel Schwierzeck 	debug("## Giving linux memsize in MB, %lu\n",
198e51a6b7aSDaniel Schwierzeck 	      (ulong)(gd->ram_size >> 20));
199ea0364f1SPeter Tyser #endif /* CONFIG_MEMSIZE_IN_BYTES */
200ea0364f1SPeter Tyser 
2016c154552SDaniel Schwierzeck 	rd_start = UNCACHED_SDRAM(images->initrd_start);
2026c154552SDaniel Schwierzeck 	rd_size = images->initrd_end - images->initrd_start;
2036c154552SDaniel Schwierzeck 
20415f8aa90SDaniel Schwierzeck 	linux_env_init();
20515f8aa90SDaniel Schwierzeck 
206ea0364f1SPeter Tyser 	linux_env_set("memsize", env_buf);
207ea0364f1SPeter Tyser 
2086c154552SDaniel Schwierzeck 	sprintf(env_buf, "0x%08lX", rd_start);
209ea0364f1SPeter Tyser 	linux_env_set("initrd_start", env_buf);
210ea0364f1SPeter Tyser 
2116c154552SDaniel Schwierzeck 	sprintf(env_buf, "0x%lX", rd_size);
212ea0364f1SPeter Tyser 	linux_env_set("initrd_size", env_buf);
213ea0364f1SPeter Tyser 
214ea0364f1SPeter Tyser 	sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
215ea0364f1SPeter Tyser 	linux_env_set("flash_start", env_buf);
216ea0364f1SPeter Tyser 
217ea0364f1SPeter Tyser 	sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
218ea0364f1SPeter Tyser 	linux_env_set("flash_size", env_buf);
219ea0364f1SPeter Tyser 
220ea0364f1SPeter Tyser 	cp = getenv("ethaddr");
221e51a6b7aSDaniel Schwierzeck 	if (cp)
222ea0364f1SPeter Tyser 		linux_env_set("ethaddr", cp);
223ea0364f1SPeter Tyser 
224ea0364f1SPeter Tyser 	cp = getenv("eth1addr");
225e51a6b7aSDaniel Schwierzeck 	if (cp)
226ea0364f1SPeter Tyser 		linux_env_set("eth1addr", cp);
227b87493f4SDaniel Schwierzeck 
228d18d49d7SPaul Burton 	if (mips_boot_malta) {
229d18d49d7SPaul Burton 		sprintf(env_buf, "%un8r", gd->baudrate);
230d18d49d7SPaul Burton 		linux_env_set("modetty0", env_buf);
231d18d49d7SPaul Burton 	}
2320ea7213fSGabor Juhos }
233ea0364f1SPeter Tyser 
234*ca65e585SDaniel Schwierzeck static void boot_prep_linux(bootm_headers_t *images)
235*ca65e585SDaniel Schwierzeck {
236*ca65e585SDaniel Schwierzeck 	if (mips_boot_env_legacy)
237*ca65e585SDaniel Schwierzeck 		linux_env_legacy(images);
238*ca65e585SDaniel Schwierzeck }
239*ca65e585SDaniel Schwierzeck 
2400ea7213fSGabor Juhos static void boot_jump_linux(bootm_headers_t *images)
2410ea7213fSGabor Juhos {
242c4b37847SDaniel Schwierzeck 	typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
243c4b37847SDaniel Schwierzeck 	kernel_entry_t kernel = (kernel_entry_t) images->ep;
244b87493f4SDaniel Schwierzeck 	ulong linux_extra = 0;
2450ea7213fSGabor Juhos 
246c4b37847SDaniel Schwierzeck 	debug("## Transferring control to Linux (at address %p) ...\n", kernel);
2470ea7213fSGabor Juhos 
2480ea7213fSGabor Juhos 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
2490ea7213fSGabor Juhos 
2507a9d109bSPaul Burton 	if (mips_boot_malta)
251b87493f4SDaniel Schwierzeck 		linux_extra = gd->ram_size;
252b87493f4SDaniel Schwierzeck 
2530ea7213fSGabor Juhos 	/* we assume that the kernel is in place */
2540ea7213fSGabor Juhos 	printf("\nStarting kernel ...\n\n");
2550ea7213fSGabor Juhos 
256b87493f4SDaniel Schwierzeck 	kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
2570ea7213fSGabor Juhos }
2580ea7213fSGabor Juhos 
2590ea7213fSGabor Juhos int do_bootm_linux(int flag, int argc, char * const argv[],
2600ea7213fSGabor Juhos 			bootm_headers_t *images)
2610ea7213fSGabor Juhos {
262c9639421SDaniel Schwierzeck 	int ret;
263c9639421SDaniel Schwierzeck 
2649c170e2eSGabor Juhos 	/* No need for those on MIPS */
26559e8cbdbSDaniel Schwierzeck 	if (flag & BOOTM_STATE_OS_BD_T)
2669c170e2eSGabor Juhos 		return -1;
2679c170e2eSGabor Juhos 
26859e8cbdbSDaniel Schwierzeck 	if (flag & BOOTM_STATE_OS_CMDLINE) {
26959e8cbdbSDaniel Schwierzeck 		boot_cmdline_linux(images);
27059e8cbdbSDaniel Schwierzeck 		return 0;
27159e8cbdbSDaniel Schwierzeck 	}
27259e8cbdbSDaniel Schwierzeck 
2739c170e2eSGabor Juhos 	if (flag & BOOTM_STATE_OS_PREP) {
2749c170e2eSGabor Juhos 		boot_prep_linux(images);
2759c170e2eSGabor Juhos 		return 0;
2769c170e2eSGabor Juhos 	}
2779c170e2eSGabor Juhos 
2789c170e2eSGabor Juhos 	if (flag & BOOTM_STATE_OS_GO) {
2799c170e2eSGabor Juhos 		boot_jump_linux(images);
2809c170e2eSGabor Juhos 		return 0;
2819c170e2eSGabor Juhos 	}
2820ea7213fSGabor Juhos 
283c9639421SDaniel Schwierzeck 	ret = boot_setup_linux(images);
284c9639421SDaniel Schwierzeck 	if (ret)
285c9639421SDaniel Schwierzeck 		return ret;
286c9639421SDaniel Schwierzeck 
28759e8cbdbSDaniel Schwierzeck 	boot_cmdline_linux(images);
2880ea7213fSGabor Juhos 	boot_prep_linux(images);
289e08634c7SGabor Juhos 	boot_jump_linux(images);
290e51a6b7aSDaniel Schwierzeck 
291ea0364f1SPeter Tyser 	/* does not return */
292ea0364f1SPeter Tyser 	return 1;
293ea0364f1SPeter Tyser }
294