blob: 0881b6db9957777e22795df9bada3bd9ef493ee4 [file] [log] [blame]
wdenkc0218802003-03-27 12:09:35 +00001/*
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>
wdenkc0218802003-03-27 12:09:35 +000026#include <image.h>
27#include <zlib.h>
28#include <asm/byteorder.h>
29#include <asm/addrspace.h>
30
Wolfgang Denkd87080b2006-03-31 18:32:53 +020031DECLARE_GLOBAL_DATA_PTR;
32
wdenkc0218802003-03-27 12:09:35 +000033#define LINUX_MAX_ENVS 256
34#define LINUX_MAX_ARGS 256
35
wdenkc0218802003-03-27 12:09:35 +000036extern image_header_t header; /* from cmd_bootm.c */
37
wdenk8bde7f72003-06-27 21:31:46 +000038extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
39
wdenkc0218802003-03-27 12:09:35 +000040static int linux_argc;
41static char ** linux_argv;
42
43static char ** linux_env;
44static char * linux_env_p;
45static int linux_env_idx;
46
47static void linux_params_init (ulong start, char * commandline);
48static void linux_env_set (char * env_name, char * env_val);
49
50
wdenk5da627a2003-10-09 20:09:04 +000051void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
52 ulong addr, ulong * len_ptr, int verify)
wdenkc0218802003-03-27 12:09:35 +000053{
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010054 ulong len = 0;
wdenk5da627a2003-10-09 20:09:04 +000055 ulong initrd_start, initrd_end;
56 ulong data;
57 void (*theKernel) (int, char **, char **, int *);
58 image_header_t *hdr = &header;
59 char *commandline = getenv ("bootargs");
60 char env_buf[12];
wdenkc0218802003-03-27 12:09:35 +000061
wdenk5da627a2003-10-09 20:09:04 +000062 theKernel =
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010063 (void (*)(int, char **, char **, int *))image_get_ep (hdr);
wdenkc0218802003-03-27 12:09:35 +000064
65 /*
wdenk5da627a2003-10-09 20:09:04 +000066 * Check if there is an initrd image
wdenkc0218802003-03-27 12:09:35 +000067 */
wdenk5da627a2003-10-09 20:09:04 +000068 if (argc >= 3) {
Heiko Schocherfad63402007-07-13 09:54:17 +020069 show_boot_progress (9);
wdenkc0218802003-03-27 12:09:35 +000070
wdenk5da627a2003-10-09 20:09:04 +000071 addr = simple_strtoul (argv[2], NULL, 16);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010072 hdr = (image_header_t *)addr;
wdenkc0218802003-03-27 12:09:35 +000073
wdenk5da627a2003-10-09 20:09:04 +000074 printf ("## Loading Ramdisk Image at %08lx ...\n", addr);
75
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010076 if (!image_check_magic (hdr)) {
wdenk5da627a2003-10-09 20:09:04 +000077 printf ("Bad Magic Number\n");
Heiko Schocherfad63402007-07-13 09:54:17 +020078 show_boot_progress (-10);
wdenk5da627a2003-10-09 20:09:04 +000079 do_reset (cmdtp, flag, argc, argv);
80 }
81
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010082 if (!image_check_hcrc (hdr)) {
wdenk5da627a2003-10-09 20:09:04 +000083 printf ("Bad Header Checksum\n");
Heiko Schocherfad63402007-07-13 09:54:17 +020084 show_boot_progress (-11);
wdenk5da627a2003-10-09 20:09:04 +000085 do_reset (cmdtp, flag, argc, argv);
86 }
87
Heiko Schocherfad63402007-07-13 09:54:17 +020088 show_boot_progress (10);
wdenk5da627a2003-10-09 20:09:04 +000089
90 print_image_hdr (hdr);
91
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010092 data = image_get_data (hdr);
93 len = image_get_data_size (hdr);
wdenk5da627a2003-10-09 20:09:04 +000094
95 if (verify) {
wdenk5da627a2003-10-09 20:09:04 +000096 printf (" Verifying Checksum ... ");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010097 if (!image_check_dcrc (hdr)) {
wdenk5da627a2003-10-09 20:09:04 +000098 printf ("Bad Data CRC\n");
Heiko Schocherfad63402007-07-13 09:54:17 +020099 show_boot_progress (-12);
wdenk5da627a2003-10-09 20:09:04 +0000100 do_reset (cmdtp, flag, argc, argv);
101 }
102 printf ("OK\n");
103 }
104
Heiko Schocherfad63402007-07-13 09:54:17 +0200105 show_boot_progress (11);
wdenk5da627a2003-10-09 20:09:04 +0000106
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100107 if (!image_check_os (hdr, IH_OS_LINUX) ||
108 !image_check_arch (hdr, IH_ARCH_MIPS) ||
109 !image_check_type (hdr, IH_TYPE_RAMDISK)) {
wdenk5da627a2003-10-09 20:09:04 +0000110 printf ("No Linux MIPS Ramdisk Image\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200111 show_boot_progress (-13);
wdenk5da627a2003-10-09 20:09:04 +0000112 do_reset (cmdtp, flag, argc, argv);
113 }
114
115 /*
116 * Now check if we have a multifile image
117 */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100118 } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
119 ulong tail = image_to_cpu (len_ptr[0]) % 4;
wdenk5da627a2003-10-09 20:09:04 +0000120 int i;
121
Heiko Schocherfad63402007-07-13 09:54:17 +0200122 show_boot_progress (13);
wdenk5da627a2003-10-09 20:09:04 +0000123
124 /* skip kernel length and terminator */
125 data = (ulong) (&len_ptr[2]);
126 /* skip any additional image length fields */
127 for (i = 1; len_ptr[i]; ++i)
128 data += 4;
129 /* add kernel length, and align */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100130 data += image_to_cpu (len_ptr[0]);
wdenk5da627a2003-10-09 20:09:04 +0000131 if (tail) {
132 data += 4 - tail;
133 }
134
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100135 len = image_to_cpu (len_ptr[1]);
wdenk5da627a2003-10-09 20:09:04 +0000136
137 } else {
138 /*
139 * no initrd image
140 */
Heiko Schocherfad63402007-07-13 09:54:17 +0200141 show_boot_progress (14);
wdenk5da627a2003-10-09 20:09:04 +0000142
143 data = 0;
wdenkc0218802003-03-27 12:09:35 +0000144 }
145
wdenkc0218802003-03-27 12:09:35 +0000146#ifdef DEBUG
wdenk5da627a2003-10-09 20:09:04 +0000147 if (!data) {
148 printf ("No initrd\n");
149 }
wdenkc0218802003-03-27 12:09:35 +0000150#endif
151
wdenk5da627a2003-10-09 20:09:04 +0000152 if (data) {
153 initrd_start = data;
154 initrd_end = initrd_start + len;
155 } else {
156 initrd_start = 0;
157 initrd_end = 0;
158 }
wdenkc0218802003-03-27 12:09:35 +0000159
Heiko Schocherfad63402007-07-13 09:54:17 +0200160 show_boot_progress (15);
wdenkc0218802003-03-27 12:09:35 +0000161
162#ifdef DEBUG
wdenk5da627a2003-10-09 20:09:04 +0000163 printf ("## Transferring control to Linux (at address %08lx) ...\n",
164 (ulong) theKernel);
wdenkc0218802003-03-27 12:09:35 +0000165#endif
166
wdenka2663ea2003-12-07 18:32:37 +0000167 linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline);
wdenkc0218802003-03-27 12:09:35 +0000168
wdenk5da627a2003-10-09 20:09:04 +0000169#ifdef CONFIG_MEMSIZE_IN_BYTES
170 sprintf (env_buf, "%lu", gd->ram_size);
171#ifdef DEBUG
172 printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size);
173#endif
174#else
175 sprintf (env_buf, "%lu", gd->ram_size >> 20);
176#ifdef DEBUG
177 printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20);
178#endif
179#endif /* CONFIG_MEMSIZE_IN_BYTES */
wdenkc0218802003-03-27 12:09:35 +0000180
wdenk5da627a2003-10-09 20:09:04 +0000181 linux_env_set ("memsize", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000182
wdenka2663ea2003-12-07 18:32:37 +0000183 sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
wdenk5da627a2003-10-09 20:09:04 +0000184 linux_env_set ("initrd_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000185
wdenk5da627a2003-10-09 20:09:04 +0000186 sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
187 linux_env_set ("initrd_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000188
wdenk5da627a2003-10-09 20:09:04 +0000189 sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
190 linux_env_set ("flash_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000191
wdenk5da627a2003-10-09 20:09:04 +0000192 sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
193 linux_env_set ("flash_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000194
wdenk5da627a2003-10-09 20:09:04 +0000195 /* we assume that the kernel is in place */
196 printf ("\nStarting kernel ...\n\n");
197
198 theKernel (linux_argc, linux_argv, linux_env, 0);
wdenkc0218802003-03-27 12:09:35 +0000199}
200
wdenk5da627a2003-10-09 20:09:04 +0000201static void linux_params_init (ulong start, char *line)
wdenkc0218802003-03-27 12:09:35 +0000202{
wdenk5da627a2003-10-09 20:09:04 +0000203 char *next, *quote, *argp;
wdenkc0218802003-03-27 12:09:35 +0000204
wdenk5da627a2003-10-09 20:09:04 +0000205 linux_argc = 1;
206 linux_argv = (char **) start;
207 linux_argv[0] = 0;
208 argp = (char *) (linux_argv + LINUX_MAX_ARGS);
wdenkc0218802003-03-27 12:09:35 +0000209
wdenk5da627a2003-10-09 20:09:04 +0000210 next = line;
wdenkc0218802003-03-27 12:09:35 +0000211
wdenk5da627a2003-10-09 20:09:04 +0000212 while (line && *line && linux_argc < LINUX_MAX_ARGS) {
213 quote = strchr (line, '"');
214 next = strchr (line, ' ');
wdenkc0218802003-03-27 12:09:35 +0000215
wdenk5da627a2003-10-09 20:09:04 +0000216 while (next != NULL && quote != NULL && quote < next) {
217 /* we found a left quote before the next blank
218 * now we have to find the matching right quote
219 */
220 next = strchr (quote + 1, '"');
221 if (next != NULL) {
222 quote = strchr (next + 1, '"');
223 next = strchr (next + 1, ' ');
224 }
225 }
226
227 if (next == NULL) {
228 next = line + strlen (line);
229 }
230
231 linux_argv[linux_argc] = argp;
232 memcpy (argp, line, next - line);
233 argp[next - line] = 0;
234
235 argp += next - line + 1;
236 linux_argc++;
237
238 if (*next)
239 next++;
240
241 line = next;
wdenk8bde7f72003-06-27 21:31:46 +0000242 }
243
wdenk5da627a2003-10-09 20:09:04 +0000244 linux_env = (char **) (((ulong) argp + 15) & ~15);
245 linux_env[0] = 0;
246 linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
247 linux_env_idx = 0;
wdenkc0218802003-03-27 12:09:35 +0000248}
249
wdenk5da627a2003-10-09 20:09:04 +0000250static void linux_env_set (char *env_name, char *env_val)
wdenkc0218802003-03-27 12:09:35 +0000251{
wdenk5da627a2003-10-09 20:09:04 +0000252 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
253 linux_env[linux_env_idx] = linux_env_p;
wdenkc0218802003-03-27 12:09:35 +0000254
wdenk5da627a2003-10-09 20:09:04 +0000255 strcpy (linux_env_p, env_name);
256 linux_env_p += strlen (env_name);
wdenkc0218802003-03-27 12:09:35 +0000257
wdenk5da627a2003-10-09 20:09:04 +0000258 strcpy (linux_env_p, "=");
259 linux_env_p += 1;
wdenkc0218802003-03-27 12:09:35 +0000260
wdenk5da627a2003-10-09 20:09:04 +0000261 strcpy (linux_env_p, env_val);
262 linux_env_p += strlen (env_val);
wdenk8bde7f72003-06-27 21:31:46 +0000263
wdenk5da627a2003-10-09 20:09:04 +0000264 linux_env_p++;
265 linux_env[++linux_env_idx] = 0;
266 }
wdenkc0218802003-03-27 12:09:35 +0000267}