blob: 7ea7571a816816f5f69e425548ee29906e732537 [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
wdenk8bde7f72003-06-27 21:31:46 +000036extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
37
wdenkc0218802003-03-27 12:09:35 +000038static int linux_argc;
39static char ** linux_argv;
40
41static char ** linux_env;
42static char * linux_env_p;
43static int linux_env_idx;
44
45static void linux_params_init (ulong start, char * commandline);
46static void linux_env_set (char * env_name, char * env_val);
47
48
wdenk5da627a2003-10-09 20:09:04 +000049void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010050 image_header_t *hdr, int verify)
wdenkc0218802003-03-27 12:09:35 +000051{
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010052 ulong rd_data, rd_len;
wdenk5da627a2003-10-09 20:09:04 +000053 ulong initrd_start, initrd_end;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010054 image_header_t *rd_hdr;
55
wdenk5da627a2003-10-09 20:09:04 +000056 void (*theKernel) (int, char **, char **, int *);
wdenk5da627a2003-10-09 20:09:04 +000057 char *commandline = getenv ("bootargs");
58 char env_buf[12];
wdenkc0218802003-03-27 12:09:35 +000059
wdenk5da627a2003-10-09 20:09:04 +000060 theKernel =
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010061 (void (*)(int, char **, char **, int *))image_get_ep (hdr);
wdenkc0218802003-03-27 12:09:35 +000062
63 /*
wdenk5da627a2003-10-09 20:09:04 +000064 * Check if there is an initrd image
wdenkc0218802003-03-27 12:09:35 +000065 */
wdenk5da627a2003-10-09 20:09:04 +000066 if (argc >= 3) {
Heiko Schocherfad63402007-07-13 09:54:17 +020067 show_boot_progress (9);
wdenkc0218802003-03-27 12:09:35 +000068
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010069 rd_hdr = (image_header_t *)simple_strtoul (argv[2], NULL, 16);
70 printf ("## Loading Ramdisk Image at %08lx ...\n", rd_hdr);
wdenkc0218802003-03-27 12:09:35 +000071
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010072 if (!image_check_magic (rd_hdr)) {
wdenk5da627a2003-10-09 20:09:04 +000073 printf ("Bad Magic Number\n");
Heiko Schocherfad63402007-07-13 09:54:17 +020074 show_boot_progress (-10);
wdenk5da627a2003-10-09 20:09:04 +000075 do_reset (cmdtp, flag, argc, argv);
76 }
77
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010078 if (!image_check_hcrc (rd_hdr)) {
wdenk5da627a2003-10-09 20:09:04 +000079 printf ("Bad Header Checksum\n");
Heiko Schocherfad63402007-07-13 09:54:17 +020080 show_boot_progress (-11);
wdenk5da627a2003-10-09 20:09:04 +000081 do_reset (cmdtp, flag, argc, argv);
82 }
83
Heiko Schocherfad63402007-07-13 09:54:17 +020084 show_boot_progress (10);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010085 print_image_hdr (rd_hdr);
wdenk5da627a2003-10-09 20:09:04 +000086
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010087 rd_data = image_get_data (rd_hdr);
88 rd_len = image_get_data_size (rd_hdr);
wdenk5da627a2003-10-09 20:09:04 +000089
90 if (verify) {
wdenk5da627a2003-10-09 20:09:04 +000091 printf (" Verifying Checksum ... ");
Marian Balakowiczf13e7b22008-01-08 18:12:17 +010092 if (!image_check_dcrc (rd_hdr)) {
wdenk5da627a2003-10-09 20:09:04 +000093 printf ("Bad Data CRC\n");
Heiko Schocherfad63402007-07-13 09:54:17 +020094 show_boot_progress (-12);
wdenk5da627a2003-10-09 20:09:04 +000095 do_reset (cmdtp, flag, argc, argv);
96 }
97 printf ("OK\n");
98 }
99
Heiko Schocherfad63402007-07-13 09:54:17 +0200100 show_boot_progress (11);
wdenk5da627a2003-10-09 20:09:04 +0000101
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100102 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
103 !image_check_arch (rd_hdr, IH_ARCH_MIPS) ||
104 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
wdenk5da627a2003-10-09 20:09:04 +0000105 printf ("No Linux MIPS Ramdisk Image\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200106 show_boot_progress (-13);
wdenk5da627a2003-10-09 20:09:04 +0000107 do_reset (cmdtp, flag, argc, argv);
108 }
109
110 /*
111 * Now check if we have a multifile image
112 */
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100113 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
114 /*
115 * Get second entry data start address and len
116 */
Heiko Schocherfad63402007-07-13 09:54:17 +0200117 show_boot_progress (13);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100118 image_multi_getimg (hdr, 1, &rd_data, &rd_len);
wdenk5da627a2003-10-09 20:09:04 +0000119 } else {
120 /*
121 * no initrd image
122 */
Heiko Schocherfad63402007-07-13 09:54:17 +0200123 show_boot_progress (14);
wdenk5da627a2003-10-09 20:09:04 +0000124
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100125 rd_data = rd_len = 0;
wdenkc0218802003-03-27 12:09:35 +0000126 }
127
wdenkc0218802003-03-27 12:09:35 +0000128#ifdef DEBUG
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100129 if (!rd_data) {
wdenk5da627a2003-10-09 20:09:04 +0000130 printf ("No initrd\n");
131 }
wdenkc0218802003-03-27 12:09:35 +0000132#endif
133
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100134 if (rd_data) {
135 initrd_start = rd_data;
136 initrd_end = initrd_start + rd_len;
wdenk5da627a2003-10-09 20:09:04 +0000137 } else {
138 initrd_start = 0;
139 initrd_end = 0;
140 }
wdenkc0218802003-03-27 12:09:35 +0000141
Heiko Schocherfad63402007-07-13 09:54:17 +0200142 show_boot_progress (15);
wdenkc0218802003-03-27 12:09:35 +0000143
144#ifdef DEBUG
wdenk5da627a2003-10-09 20:09:04 +0000145 printf ("## Transferring control to Linux (at address %08lx) ...\n",
146 (ulong) theKernel);
wdenkc0218802003-03-27 12:09:35 +0000147#endif
148
wdenka2663ea2003-12-07 18:32:37 +0000149 linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline);
wdenkc0218802003-03-27 12:09:35 +0000150
wdenk5da627a2003-10-09 20:09:04 +0000151#ifdef CONFIG_MEMSIZE_IN_BYTES
152 sprintf (env_buf, "%lu", gd->ram_size);
153#ifdef DEBUG
154 printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size);
155#endif
156#else
157 sprintf (env_buf, "%lu", gd->ram_size >> 20);
158#ifdef DEBUG
159 printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20);
160#endif
161#endif /* CONFIG_MEMSIZE_IN_BYTES */
wdenkc0218802003-03-27 12:09:35 +0000162
wdenk5da627a2003-10-09 20:09:04 +0000163 linux_env_set ("memsize", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000164
wdenka2663ea2003-12-07 18:32:37 +0000165 sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
wdenk5da627a2003-10-09 20:09:04 +0000166 linux_env_set ("initrd_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000167
wdenk5da627a2003-10-09 20:09:04 +0000168 sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
169 linux_env_set ("initrd_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000170
wdenk5da627a2003-10-09 20:09:04 +0000171 sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
172 linux_env_set ("flash_start", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000173
wdenk5da627a2003-10-09 20:09:04 +0000174 sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
175 linux_env_set ("flash_size", env_buf);
wdenkc0218802003-03-27 12:09:35 +0000176
wdenk5da627a2003-10-09 20:09:04 +0000177 /* we assume that the kernel is in place */
178 printf ("\nStarting kernel ...\n\n");
179
180 theKernel (linux_argc, linux_argv, linux_env, 0);
wdenkc0218802003-03-27 12:09:35 +0000181}
182
wdenk5da627a2003-10-09 20:09:04 +0000183static void linux_params_init (ulong start, char *line)
wdenkc0218802003-03-27 12:09:35 +0000184{
wdenk5da627a2003-10-09 20:09:04 +0000185 char *next, *quote, *argp;
wdenkc0218802003-03-27 12:09:35 +0000186
wdenk5da627a2003-10-09 20:09:04 +0000187 linux_argc = 1;
188 linux_argv = (char **) start;
189 linux_argv[0] = 0;
190 argp = (char *) (linux_argv + LINUX_MAX_ARGS);
wdenkc0218802003-03-27 12:09:35 +0000191
wdenk5da627a2003-10-09 20:09:04 +0000192 next = line;
wdenkc0218802003-03-27 12:09:35 +0000193
wdenk5da627a2003-10-09 20:09:04 +0000194 while (line && *line && linux_argc < LINUX_MAX_ARGS) {
195 quote = strchr (line, '"');
196 next = strchr (line, ' ');
wdenkc0218802003-03-27 12:09:35 +0000197
wdenk5da627a2003-10-09 20:09:04 +0000198 while (next != NULL && quote != NULL && quote < next) {
199 /* we found a left quote before the next blank
200 * now we have to find the matching right quote
201 */
202 next = strchr (quote + 1, '"');
203 if (next != NULL) {
204 quote = strchr (next + 1, '"');
205 next = strchr (next + 1, ' ');
206 }
207 }
208
209 if (next == NULL) {
210 next = line + strlen (line);
211 }
212
213 linux_argv[linux_argc] = argp;
214 memcpy (argp, line, next - line);
215 argp[next - line] = 0;
216
217 argp += next - line + 1;
218 linux_argc++;
219
220 if (*next)
221 next++;
222
223 line = next;
wdenk8bde7f72003-06-27 21:31:46 +0000224 }
225
wdenk5da627a2003-10-09 20:09:04 +0000226 linux_env = (char **) (((ulong) argp + 15) & ~15);
227 linux_env[0] = 0;
228 linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
229 linux_env_idx = 0;
wdenkc0218802003-03-27 12:09:35 +0000230}
231
wdenk5da627a2003-10-09 20:09:04 +0000232static void linux_env_set (char *env_name, char *env_val)
wdenkc0218802003-03-27 12:09:35 +0000233{
wdenk5da627a2003-10-09 20:09:04 +0000234 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
235 linux_env[linux_env_idx] = linux_env_p;
wdenkc0218802003-03-27 12:09:35 +0000236
wdenk5da627a2003-10-09 20:09:04 +0000237 strcpy (linux_env_p, env_name);
238 linux_env_p += strlen (env_name);
wdenkc0218802003-03-27 12:09:35 +0000239
wdenk5da627a2003-10-09 20:09:04 +0000240 strcpy (linux_env_p, "=");
241 linux_env_p += 1;
wdenkc0218802003-03-27 12:09:35 +0000242
wdenk5da627a2003-10-09 20:09:04 +0000243 strcpy (linux_env_p, env_val);
244 linux_env_p += strlen (env_val);
wdenk8bde7f72003-06-27 21:31:46 +0000245
wdenk5da627a2003-10-09 20:09:04 +0000246 linux_env_p++;
247 linux_env[++linux_env_idx] = 0;
248 }
wdenkc0218802003-03-27 12:09:35 +0000249}