xref: /rk3399_rockchip-uboot/arch/mips/lib/bootm.c (revision 7a9d109b00a207b481b05d8e147673da33ad1cd3)
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 <command.h>
10ea0364f1SPeter Tyser #include <image.h>
11ea0364f1SPeter Tyser #include <u-boot/zlib.h>
12ea0364f1SPeter Tyser #include <asm/byteorder.h>
13ea0364f1SPeter Tyser #include <asm/addrspace.h>
14ea0364f1SPeter Tyser 
15ea0364f1SPeter Tyser DECLARE_GLOBAL_DATA_PTR;
16ea0364f1SPeter Tyser 
17ea0364f1SPeter Tyser #define	LINUX_MAX_ENVS		256
18ea0364f1SPeter Tyser #define	LINUX_MAX_ARGS		256
19ea0364f1SPeter Tyser 
20*7a9d109bSPaul Burton #if defined(CONFIG_MALTA)
21*7a9d109bSPaul Burton #define mips_boot_malta		1
22b87493f4SDaniel Schwierzeck #else
23*7a9d109bSPaul Burton #define mips_boot_malta		0
24b87493f4SDaniel Schwierzeck #endif
25b87493f4SDaniel Schwierzeck 
26ea0364f1SPeter Tyser static int linux_argc;
27ea0364f1SPeter Tyser static char **linux_argv;
2859e8cbdbSDaniel Schwierzeck static char *linux_argp;
29ea0364f1SPeter Tyser 
30ea0364f1SPeter Tyser static char **linux_env;
31ea0364f1SPeter Tyser static char *linux_env_p;
32ea0364f1SPeter Tyser static int linux_env_idx;
33ea0364f1SPeter Tyser 
34f66cc1e3SDaniel Schwierzeck static ulong arch_get_sp(void)
35f66cc1e3SDaniel Schwierzeck {
36f66cc1e3SDaniel Schwierzeck 	ulong ret;
37f66cc1e3SDaniel Schwierzeck 
38f66cc1e3SDaniel Schwierzeck 	__asm__ __volatile__("move %0, $sp" : "=r"(ret) : );
39f66cc1e3SDaniel Schwierzeck 
40f66cc1e3SDaniel Schwierzeck 	return ret;
41f66cc1e3SDaniel Schwierzeck }
42f66cc1e3SDaniel Schwierzeck 
43f66cc1e3SDaniel Schwierzeck void arch_lmb_reserve(struct lmb *lmb)
44f66cc1e3SDaniel Schwierzeck {
45f66cc1e3SDaniel Schwierzeck 	ulong sp;
46f66cc1e3SDaniel Schwierzeck 
47f66cc1e3SDaniel Schwierzeck 	sp = arch_get_sp();
48f66cc1e3SDaniel Schwierzeck 	debug("## Current stack ends at 0x%08lx\n", sp);
49f66cc1e3SDaniel Schwierzeck 
50f66cc1e3SDaniel Schwierzeck 	/* adjust sp by 4K to be safe */
51f66cc1e3SDaniel Schwierzeck 	sp -= 4096;
52f66cc1e3SDaniel Schwierzeck 	lmb_reserve(lmb, sp, CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp);
53f66cc1e3SDaniel Schwierzeck }
54f66cc1e3SDaniel Schwierzeck 
5559e8cbdbSDaniel Schwierzeck static void linux_cmdline_init(void)
5659e8cbdbSDaniel Schwierzeck {
5759e8cbdbSDaniel Schwierzeck 	linux_argc = 1;
5859e8cbdbSDaniel Schwierzeck 	linux_argv = (char **)UNCACHED_SDRAM(gd->bd->bi_boot_params);
5959e8cbdbSDaniel Schwierzeck 	linux_argv[0] = 0;
6059e8cbdbSDaniel Schwierzeck 	linux_argp = (char *)(linux_argv + LINUX_MAX_ARGS);
6159e8cbdbSDaniel Schwierzeck }
6259e8cbdbSDaniel Schwierzeck 
6359e8cbdbSDaniel Schwierzeck static void linux_cmdline_set(const char *value, size_t len)
6459e8cbdbSDaniel Schwierzeck {
6559e8cbdbSDaniel Schwierzeck 	linux_argv[linux_argc] = linux_argp;
6659e8cbdbSDaniel Schwierzeck 	memcpy(linux_argp, value, len);
6759e8cbdbSDaniel Schwierzeck 	linux_argp[len] = 0;
6859e8cbdbSDaniel Schwierzeck 
6959e8cbdbSDaniel Schwierzeck 	linux_argp += len + 1;
7059e8cbdbSDaniel Schwierzeck 	linux_argc++;
7159e8cbdbSDaniel Schwierzeck }
7259e8cbdbSDaniel Schwierzeck 
7359e8cbdbSDaniel Schwierzeck static void linux_cmdline_dump(void)
7459e8cbdbSDaniel Schwierzeck {
7559e8cbdbSDaniel Schwierzeck 	int i;
7659e8cbdbSDaniel Schwierzeck 
7759e8cbdbSDaniel Schwierzeck 	debug("## cmdline argv at 0x%p, argp at 0x%p\n",
7859e8cbdbSDaniel Schwierzeck 	      linux_argv, linux_argp);
7959e8cbdbSDaniel Schwierzeck 
8059e8cbdbSDaniel Schwierzeck 	for (i = 1; i < linux_argc; i++)
8159e8cbdbSDaniel Schwierzeck 		debug("   arg %03d: %s\n", i, linux_argv[i]);
8259e8cbdbSDaniel Schwierzeck }
8359e8cbdbSDaniel Schwierzeck 
8459e8cbdbSDaniel Schwierzeck static void boot_cmdline_linux(bootm_headers_t *images)
8559e8cbdbSDaniel Schwierzeck {
8659e8cbdbSDaniel Schwierzeck 	const char *bootargs, *next, *quote;
8759e8cbdbSDaniel Schwierzeck 
8859e8cbdbSDaniel Schwierzeck 	linux_cmdline_init();
8959e8cbdbSDaniel Schwierzeck 
9059e8cbdbSDaniel Schwierzeck 	bootargs = getenv("bootargs");
9159e8cbdbSDaniel Schwierzeck 	if (!bootargs)
9259e8cbdbSDaniel Schwierzeck 		return;
9359e8cbdbSDaniel Schwierzeck 
9459e8cbdbSDaniel Schwierzeck 	next = bootargs;
9559e8cbdbSDaniel Schwierzeck 
9659e8cbdbSDaniel Schwierzeck 	while (bootargs && *bootargs && linux_argc < LINUX_MAX_ARGS) {
9759e8cbdbSDaniel Schwierzeck 		quote = strchr(bootargs, '"');
9859e8cbdbSDaniel Schwierzeck 		next = strchr(bootargs, ' ');
9959e8cbdbSDaniel Schwierzeck 
10059e8cbdbSDaniel Schwierzeck 		while (next && quote && quote < next) {
10159e8cbdbSDaniel Schwierzeck 			/*
10259e8cbdbSDaniel Schwierzeck 			 * we found a left quote before the next blank
10359e8cbdbSDaniel Schwierzeck 			 * now we have to find the matching right quote
10459e8cbdbSDaniel Schwierzeck 			 */
10559e8cbdbSDaniel Schwierzeck 			next = strchr(quote + 1, '"');
10659e8cbdbSDaniel Schwierzeck 			if (next) {
10759e8cbdbSDaniel Schwierzeck 				quote = strchr(next + 1, '"');
10859e8cbdbSDaniel Schwierzeck 				next = strchr(next + 1, ' ');
10959e8cbdbSDaniel Schwierzeck 			}
11059e8cbdbSDaniel Schwierzeck 		}
11159e8cbdbSDaniel Schwierzeck 
11259e8cbdbSDaniel Schwierzeck 		if (!next)
11359e8cbdbSDaniel Schwierzeck 			next = bootargs + strlen(bootargs);
11459e8cbdbSDaniel Schwierzeck 
11559e8cbdbSDaniel Schwierzeck 		linux_cmdline_set(bootargs, next - bootargs);
11659e8cbdbSDaniel Schwierzeck 
11759e8cbdbSDaniel Schwierzeck 		if (*next)
11859e8cbdbSDaniel Schwierzeck 			next++;
11959e8cbdbSDaniel Schwierzeck 
12059e8cbdbSDaniel Schwierzeck 		bootargs = next;
12159e8cbdbSDaniel Schwierzeck 	}
12259e8cbdbSDaniel Schwierzeck 
12359e8cbdbSDaniel Schwierzeck 	linux_cmdline_dump();
12459e8cbdbSDaniel Schwierzeck }
12559e8cbdbSDaniel Schwierzeck 
12615f8aa90SDaniel Schwierzeck static void linux_env_init(void)
12715f8aa90SDaniel Schwierzeck {
12815f8aa90SDaniel Schwierzeck 	linux_env = (char **)(((ulong) linux_argp + 15) & ~15);
12915f8aa90SDaniel Schwierzeck 	linux_env[0] = 0;
13015f8aa90SDaniel Schwierzeck 	linux_env_p = (char *)(linux_env + LINUX_MAX_ENVS);
13115f8aa90SDaniel Schwierzeck 	linux_env_idx = 0;
13215f8aa90SDaniel Schwierzeck }
13315f8aa90SDaniel Schwierzeck 
13415f8aa90SDaniel Schwierzeck static void linux_env_set(const char *env_name, const char *env_val)
13515f8aa90SDaniel Schwierzeck {
13615f8aa90SDaniel Schwierzeck 	if (linux_env_idx < LINUX_MAX_ENVS - 1) {
13715f8aa90SDaniel Schwierzeck 		linux_env[linux_env_idx] = linux_env_p;
13815f8aa90SDaniel Schwierzeck 
13915f8aa90SDaniel Schwierzeck 		strcpy(linux_env_p, env_name);
14015f8aa90SDaniel Schwierzeck 		linux_env_p += strlen(env_name);
14115f8aa90SDaniel Schwierzeck 
142*7a9d109bSPaul Burton 		if (mips_boot_malta) {
143b87493f4SDaniel Schwierzeck 			linux_env_p++;
144b87493f4SDaniel Schwierzeck 			linux_env[++linux_env_idx] = linux_env_p;
145b87493f4SDaniel Schwierzeck 		} else {
14615f8aa90SDaniel Schwierzeck 			*linux_env_p++ = '=';
147b87493f4SDaniel Schwierzeck 		}
14815f8aa90SDaniel Schwierzeck 
14915f8aa90SDaniel Schwierzeck 		strcpy(linux_env_p, env_val);
15015f8aa90SDaniel Schwierzeck 		linux_env_p += strlen(env_val);
15115f8aa90SDaniel Schwierzeck 
15215f8aa90SDaniel Schwierzeck 		linux_env_p++;
15315f8aa90SDaniel Schwierzeck 		linux_env[++linux_env_idx] = 0;
15415f8aa90SDaniel Schwierzeck 	}
15515f8aa90SDaniel Schwierzeck }
15615f8aa90SDaniel Schwierzeck 
1570ea7213fSGabor Juhos static void boot_prep_linux(bootm_headers_t *images)
158ea0364f1SPeter Tyser {
159ea0364f1SPeter Tyser 	char env_buf[12];
16015f8aa90SDaniel Schwierzeck 	const char *cp;
1616c154552SDaniel Schwierzeck 	ulong rd_start, rd_size;
162ea0364f1SPeter Tyser 
163ea0364f1SPeter Tyser #ifdef CONFIG_MEMSIZE_IN_BYTES
164ea0364f1SPeter Tyser 	sprintf(env_buf, "%lu", (ulong)gd->ram_size);
165ea0364f1SPeter Tyser 	debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
166ea0364f1SPeter Tyser #else
167ea0364f1SPeter Tyser 	sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
168e51a6b7aSDaniel Schwierzeck 	debug("## Giving linux memsize in MB, %lu\n",
169e51a6b7aSDaniel Schwierzeck 	      (ulong)(gd->ram_size >> 20));
170ea0364f1SPeter Tyser #endif /* CONFIG_MEMSIZE_IN_BYTES */
171ea0364f1SPeter Tyser 
1726c154552SDaniel Schwierzeck 	rd_start = UNCACHED_SDRAM(images->initrd_start);
1736c154552SDaniel Schwierzeck 	rd_size = images->initrd_end - images->initrd_start;
1746c154552SDaniel Schwierzeck 
17515f8aa90SDaniel Schwierzeck 	linux_env_init();
17615f8aa90SDaniel Schwierzeck 
177ea0364f1SPeter Tyser 	linux_env_set("memsize", env_buf);
178ea0364f1SPeter Tyser 
1796c154552SDaniel Schwierzeck 	sprintf(env_buf, "0x%08lX", rd_start);
180ea0364f1SPeter Tyser 	linux_env_set("initrd_start", env_buf);
181ea0364f1SPeter Tyser 
1826c154552SDaniel Schwierzeck 	sprintf(env_buf, "0x%lX", rd_size);
183ea0364f1SPeter Tyser 	linux_env_set("initrd_size", env_buf);
184ea0364f1SPeter Tyser 
185ea0364f1SPeter Tyser 	sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
186ea0364f1SPeter Tyser 	linux_env_set("flash_start", env_buf);
187ea0364f1SPeter Tyser 
188ea0364f1SPeter Tyser 	sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
189ea0364f1SPeter Tyser 	linux_env_set("flash_size", env_buf);
190ea0364f1SPeter Tyser 
191ea0364f1SPeter Tyser 	cp = getenv("ethaddr");
192e51a6b7aSDaniel Schwierzeck 	if (cp)
193ea0364f1SPeter Tyser 		linux_env_set("ethaddr", cp);
194ea0364f1SPeter Tyser 
195ea0364f1SPeter Tyser 	cp = getenv("eth1addr");
196e51a6b7aSDaniel Schwierzeck 	if (cp)
197ea0364f1SPeter Tyser 		linux_env_set("eth1addr", cp);
198b87493f4SDaniel Schwierzeck 
199*7a9d109bSPaul Burton 	if (mips_boot_malta)
200b87493f4SDaniel Schwierzeck 		linux_env_set("modetty0", "38400n8r");
2010ea7213fSGabor Juhos }
202ea0364f1SPeter Tyser 
2030ea7213fSGabor Juhos static void boot_jump_linux(bootm_headers_t *images)
2040ea7213fSGabor Juhos {
205c4b37847SDaniel Schwierzeck 	typedef void __noreturn (*kernel_entry_t)(int, ulong, ulong, ulong);
206c4b37847SDaniel Schwierzeck 	kernel_entry_t kernel = (kernel_entry_t) images->ep;
207b87493f4SDaniel Schwierzeck 	ulong linux_extra = 0;
2080ea7213fSGabor Juhos 
209c4b37847SDaniel Schwierzeck 	debug("## Transferring control to Linux (at address %p) ...\n", kernel);
2100ea7213fSGabor Juhos 
2110ea7213fSGabor Juhos 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
2120ea7213fSGabor Juhos 
213*7a9d109bSPaul Burton 	if (mips_boot_malta)
214b87493f4SDaniel Schwierzeck 		linux_extra = gd->ram_size;
215b87493f4SDaniel Schwierzeck 
2160ea7213fSGabor Juhos 	/* we assume that the kernel is in place */
2170ea7213fSGabor Juhos 	printf("\nStarting kernel ...\n\n");
2180ea7213fSGabor Juhos 
219b87493f4SDaniel Schwierzeck 	kernel(linux_argc, (ulong)linux_argv, (ulong)linux_env, linux_extra);
2200ea7213fSGabor Juhos }
2210ea7213fSGabor Juhos 
2220ea7213fSGabor Juhos int do_bootm_linux(int flag, int argc, char * const argv[],
2230ea7213fSGabor Juhos 			bootm_headers_t *images)
2240ea7213fSGabor Juhos {
2259c170e2eSGabor Juhos 	/* No need for those on MIPS */
22659e8cbdbSDaniel Schwierzeck 	if (flag & BOOTM_STATE_OS_BD_T)
2279c170e2eSGabor Juhos 		return -1;
2289c170e2eSGabor Juhos 
22959e8cbdbSDaniel Schwierzeck 	if (flag & BOOTM_STATE_OS_CMDLINE) {
23059e8cbdbSDaniel Schwierzeck 		boot_cmdline_linux(images);
23159e8cbdbSDaniel Schwierzeck 		return 0;
23259e8cbdbSDaniel Schwierzeck 	}
23359e8cbdbSDaniel Schwierzeck 
2349c170e2eSGabor Juhos 	if (flag & BOOTM_STATE_OS_PREP) {
2359c170e2eSGabor Juhos 		boot_prep_linux(images);
2369c170e2eSGabor Juhos 		return 0;
2379c170e2eSGabor Juhos 	}
2389c170e2eSGabor Juhos 
2399c170e2eSGabor Juhos 	if (flag & BOOTM_STATE_OS_GO) {
2409c170e2eSGabor Juhos 		boot_jump_linux(images);
2419c170e2eSGabor Juhos 		return 0;
2429c170e2eSGabor Juhos 	}
2430ea7213fSGabor Juhos 
24459e8cbdbSDaniel Schwierzeck 	boot_cmdline_linux(images);
2450ea7213fSGabor Juhos 	boot_prep_linux(images);
246e08634c7SGabor Juhos 	boot_jump_linux(images);
247e51a6b7aSDaniel Schwierzeck 
248ea0364f1SPeter Tyser 	/* does not return */
249ea0364f1SPeter Tyser 	return 1;
250ea0364f1SPeter Tyser }
251