xref: /rk3399_rockchip-uboot/arch/mips/lib/bootm.c (revision 0ea7213f63f580aa679a36795831dabd35d786aa)
1 /*
2  * (C) Copyright 2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 
24 #include <common.h>
25 #include <command.h>
26 #include <image.h>
27 #include <u-boot/zlib.h>
28 #include <asm/byteorder.h>
29 #include <asm/addrspace.h>
30 
31 DECLARE_GLOBAL_DATA_PTR;
32 
33 #define	LINUX_MAX_ENVS		256
34 #define	LINUX_MAX_ARGS		256
35 
36 static int linux_argc;
37 static char **linux_argv;
38 
39 static char **linux_env;
40 static char *linux_env_p;
41 static int linux_env_idx;
42 
43 static void linux_params_init(ulong start, char *commandline);
44 static void linux_env_set(char *env_name, char *env_val);
45 
46 static void boot_prep_linux(bootm_headers_t *images)
47 {
48 	char *commandline = getenv("bootargs");
49 	char env_buf[12];
50 	char *cp;
51 
52 	linux_params_init(UNCACHED_SDRAM(gd->bd->bi_boot_params), commandline);
53 
54 #ifdef CONFIG_MEMSIZE_IN_BYTES
55 	sprintf(env_buf, "%lu", (ulong)gd->ram_size);
56 	debug("## Giving linux memsize in bytes, %lu\n", (ulong)gd->ram_size);
57 #else
58 	sprintf(env_buf, "%lu", (ulong)(gd->ram_size >> 20));
59 	debug("## Giving linux memsize in MB, %lu\n",
60 		(ulong)(gd->ram_size >> 20));
61 #endif /* CONFIG_MEMSIZE_IN_BYTES */
62 
63 	linux_env_set("memsize", env_buf);
64 
65 	sprintf(env_buf, "0x%08X", (uint) UNCACHED_SDRAM(images->rd_start));
66 	linux_env_set("initrd_start", env_buf);
67 
68 	sprintf(env_buf, "0x%X", (uint) (images->rd_end - images->rd_start));
69 	linux_env_set("initrd_size", env_buf);
70 
71 	sprintf(env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
72 	linux_env_set("flash_start", env_buf);
73 
74 	sprintf(env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
75 	linux_env_set("flash_size", env_buf);
76 
77 	cp = getenv("ethaddr");
78 	if (cp)
79 		linux_env_set("ethaddr", cp);
80 
81 	cp = getenv("eth1addr");
82 	if (cp)
83 		linux_env_set("eth1addr", cp);
84 }
85 
86 static void boot_jump_linux(bootm_headers_t *images)
87 {
88 	void (*theKernel) (int, char **, char **, int *);
89 
90 	/* find kernel entry point */
91 	theKernel = (void (*)(int, char **, char **, int *))images->ep;
92 
93 	debug("## Transferring control to Linux (at address %08lx) ...\n",
94 		(ulong) theKernel);
95 
96 	bootstage_mark(BOOTSTAGE_ID_RUN_OS);
97 
98 	/* we assume that the kernel is in place */
99 	printf("\nStarting kernel ...\n\n");
100 
101 	theKernel(linux_argc, linux_argv, linux_env, 0);
102 }
103 
104 int do_bootm_linux(int flag, int argc, char * const argv[],
105 			bootm_headers_t *images)
106 {
107 	if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
108 		return 1;
109 
110 	boot_prep_linux(images);
111 	boot_jump_linux(images);
112 
113 	/* does not return */
114 	return 1;
115 }
116 
117 static void linux_params_init(ulong start, char *line)
118 {
119 	char *next, *quote, *argp;
120 
121 	linux_argc = 1;
122 	linux_argv = (char **) start;
123 	linux_argv[0] = 0;
124 	argp = (char *) (linux_argv + LINUX_MAX_ARGS);
125 
126 	next = line;
127 
128 	while (line && *line && linux_argc < LINUX_MAX_ARGS) {
129 		quote = strchr(line, '"');
130 		next = strchr(line, ' ');
131 
132 		while (next && quote && quote < next) {
133 			/* we found a left quote before the next blank
134 			 * now we have to find the matching right quote
135 			 */
136 			next = strchr(quote + 1, '"');
137 			if (next) {
138 				quote = strchr(next + 1, '"');
139 				next = strchr(next + 1, ' ');
140 			}
141 		}
142 
143 		if (!next)
144 			next = line + strlen(line);
145 
146 		linux_argv[linux_argc] = argp;
147 		memcpy(argp, line, next - line);
148 		argp[next - line] = 0;
149 
150 		argp += next - line + 1;
151 		linux_argc++;
152 
153 		if (*next)
154 			next++;
155 
156 		line = next;
157 	}
158 
159 	linux_env = (char **) (((ulong) argp + 15) & ~15);
160 	linux_env[0] = 0;
161 	linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
162 	linux_env_idx = 0;
163 }
164 
165 static void linux_env_set(char *env_name, char *env_val)
166 {
167 	if (linux_env_idx < LINUX_MAX_ENVS - 1) {
168 		linux_env[linux_env_idx] = linux_env_p;
169 
170 		strcpy(linux_env_p, env_name);
171 		linux_env_p += strlen(env_name);
172 
173 		strcpy(linux_env_p, "=");
174 		linux_env_p += 1;
175 
176 		strcpy(linux_env_p, env_val);
177 		linux_env_p += strlen(env_val);
178 
179 		linux_env_p++;
180 		linux_env[++linux_env_idx] = 0;
181 	}
182 }
183