blob: f87f56ea8f2ef9b96ec7581a2b042a8330253cd8 [file] [log] [blame]
wdenk4e5ca3e2003-12-08 01:34:36 +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
wdenkbf9e3b32004-02-12 00:47:09 +000015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenk4e5ca3e2003-12-08 01:34:36 +000016 * 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
wdenkbf9e3b32004-02-12 00:47:09 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
wdenk4e5ca3e2003-12-08 01:34:36 +000021 *
22 */
23
24#include <common.h>
25#include <command.h>
wdenk4e5ca3e2003-12-08 01:34:36 +000026#include <image.h>
27#include <zlib.h>
28#include <asm/byteorder.h>
29
Wolfgang Denkd87080b2006-03-31 18:32:53 +020030DECLARE_GLOBAL_DATA_PTR;
31
wdenk4e5ca3e2003-12-08 01:34:36 +000032#define PHYSADDR(x) x
33
wdenkbf9e3b32004-02-12 00:47:09 +000034#define LINUX_MAX_ENVS 256
35#define LINUX_MAX_ARGS 256
wdenk4e5ca3e2003-12-08 01:34:36 +000036
37#ifdef CONFIG_SHOW_BOOT_PROGRESS
38# include <status_led.h>
39# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
40#else
41# define SHOW_BOOT_PROGRESS(arg)
42#endif
43
44extern image_header_t header; /* from cmd_bootm.c */
45
wdenkbf9e3b32004-02-12 00:47:09 +000046extern int do_reset (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]);
47
wdenk4e5ca3e2003-12-08 01:34:36 +000048static int linux_argc;
49static char **linux_argv;
50
51static char **linux_env;
52static char *linux_env_p;
53static int linux_env_idx;
54
55static void linux_params_init (ulong start, char *commandline);
56static void linux_env_set (char *env_name, char *env_val);
57
wdenk4e5ca3e2003-12-08 01:34:36 +000058void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
59 ulong addr, ulong * len_ptr, int verify)
60{
wdenk4e5ca3e2003-12-08 01:34:36 +000061 ulong len = 0, checksum;
62 ulong initrd_start, initrd_end;
63 ulong data;
64 void (*theKernel) (int, char **, char **, int *);
65 image_header_t *hdr = &header;
66 char *commandline = getenv ("bootargs");
67 char env_buf[12];
68
69 theKernel =
70 (void (*)(int, char **, char **, int *)) ntohl (hdr->ih_ep);
71
72 /*
73 * Check if there is an initrd image
74 */
75 if (argc >= 3) {
76 SHOW_BOOT_PROGRESS (9);
77
78 addr = simple_strtoul (argv[2], NULL, 16);
79
80 printf ("## Loading Ramdisk Image at %08lx ...\n", addr);
81
82 /* Copy header so we can blank CRC field for re-calculation */
83 memcpy (&header, (char *) addr, sizeof (image_header_t));
84
85 if (ntohl (hdr->ih_magic) != IH_MAGIC) {
86 printf ("Bad Magic Number\n");
87 SHOW_BOOT_PROGRESS (-10);
88 do_reset (cmdtp, flag, argc, argv);
89 }
90
91 data = (ulong) & header;
92 len = sizeof (image_header_t);
93
94 checksum = ntohl (hdr->ih_hcrc);
95 hdr->ih_hcrc = 0;
96
97 if (crc32 (0, (char *) data, len) != checksum) {
98 printf ("Bad Header Checksum\n");
99 SHOW_BOOT_PROGRESS (-11);
100 do_reset (cmdtp, flag, argc, argv);
101 }
102
103 SHOW_BOOT_PROGRESS (10);
104
105 print_image_hdr (hdr);
106
107 data = addr + sizeof (image_header_t);
108 len = ntohl (hdr->ih_size);
109
110 if (verify) {
111 ulong csum = 0;
112
113 printf (" Verifying Checksum ... ");
114 csum = crc32 (0, (char *) data, len);
115 if (csum != ntohl (hdr->ih_dcrc)) {
116 printf ("Bad Data CRC\n");
117 SHOW_BOOT_PROGRESS (-12);
118 do_reset (cmdtp, flag, argc, argv);
119 }
120 printf ("OK\n");
121 }
122
123 SHOW_BOOT_PROGRESS (11);
124
125 if ((hdr->ih_os != IH_OS_LINUX) ||
wdenkbf9e3b32004-02-12 00:47:09 +0000126 (hdr->ih_arch != IH_CPU_M68K) ||
wdenk4e5ca3e2003-12-08 01:34:36 +0000127 (hdr->ih_type != IH_TYPE_RAMDISK)) {
wdenkbf9e3b32004-02-12 00:47:09 +0000128 printf ("No Linux M68K Ramdisk Image\n");
wdenk4e5ca3e2003-12-08 01:34:36 +0000129 SHOW_BOOT_PROGRESS (-13);
130 do_reset (cmdtp, flag, argc, argv);
131 }
132
133 /*
134 * Now check if we have a multifile image
135 */
136 } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
137 ulong tail = ntohl (len_ptr[0]) % 4;
138 int i;
139
140 SHOW_BOOT_PROGRESS (13);
141
142 /* skip kernel length and terminator */
143 data = (ulong) (&len_ptr[2]);
144 /* skip any additional image length fields */
145 for (i = 1; len_ptr[i]; ++i)
146 data += 4;
147 /* add kernel length, and align */
148 data += ntohl (len_ptr[0]);
149 if (tail) {
150 data += 4 - tail;
151 }
152
153 len = ntohl (len_ptr[1]);
154
155 } else {
156 /*
157 * no initrd image
158 */
159 SHOW_BOOT_PROGRESS (14);
160
161 data = 0;
162 }
163
164#ifdef DEBUG
165 if (!data) {
166 printf ("No initrd\n");
167 }
168#endif
169
170 if (data) {
171 initrd_start = data;
172 initrd_end = initrd_start + len;
173 } else {
174 initrd_start = 0;
175 initrd_end = 0;
176 }
177
178 SHOW_BOOT_PROGRESS (15);
179
180#ifdef DEBUG
181 printf ("## Transferring control to Linux (at address %08lx) ...\n",
182 (ulong) theKernel);
183#endif
184
185 linux_params_init (PHYSADDR (gd->bd->bi_boot_params), commandline);
186
187 sprintf (env_buf, "%lu", gd->ram_size >> 20);
188 linux_env_set ("memsize", env_buf);
189
190 sprintf (env_buf, "0x%08X", (uint) PHYSADDR (initrd_start));
191 linux_env_set ("initrd_start", env_buf);
192
193 sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
194 linux_env_set ("initrd_size", env_buf);
195
196 sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
197 linux_env_set ("flash_start", env_buf);
198
199 sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
200 linux_env_set ("flash_size", env_buf);
201
202 /* we assume that the kernel is in place */
203 printf ("\nStarting kernel ...\n\n");
204
205 theKernel (linux_argc, linux_argv, linux_env, 0);
206}
207
208static void linux_params_init (ulong start, char *line)
209{
210 char *next, *quote, *argp;
211
212 linux_argc = 1;
213 linux_argv = (char **) start;
214 linux_argv[0] = 0;
215 argp = (char *) (linux_argv + LINUX_MAX_ARGS);
216
217 next = line;
218
219 while (line && *line && linux_argc < LINUX_MAX_ARGS) {
220 quote = strchr (line, '"');
221 next = strchr (line, ' ');
222
223 while (next != NULL && quote != NULL && quote < next) {
224 /* we found a left quote before the next blank
225 * now we have to find the matching right quote
226 */
227 next = strchr (quote + 1, '"');
228 if (next != NULL) {
229 quote = strchr (next + 1, '"');
230 next = strchr (next + 1, ' ');
231 }
232 }
233
234 if (next == NULL) {
235 next = line + strlen (line);
236 }
237
238 linux_argv[linux_argc] = argp;
239 memcpy (argp, line, next - line);
240 argp[next - line] = 0;
241
242 argp += next - line + 1;
243 linux_argc++;
244
245 if (*next)
246 next++;
247
248 line = next;
249 }
250
251 linux_env = (char **) (((ulong) argp + 15) & ~15);
252 linux_env[0] = 0;
253 linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
254 linux_env_idx = 0;
255}
256
257static void linux_env_set (char *env_name, char *env_val)
258{
259 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
260 linux_env[linux_env_idx] = linux_env_p;
261
262 strcpy (linux_env_p, env_name);
263 linux_env_p += strlen (env_name);
264
265 strcpy (linux_env_p, "=");
266 linux_env_p += 1;
267
268 strcpy (linux_env_p, env_val);
269 linux_env_p += strlen (env_val);
270
271 linux_env_p++;
272 linux_env[++linux_env_idx] = 0;
273 }
274}