blob: 6c194f80aa770b7d6654f7751e831f062bfcce4a [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
wdenk4e5ca3e2003-12-08 01:34:36 +000037extern image_header_t header; /* from cmd_bootm.c */
38
wdenkbf9e3b32004-02-12 00:47:09 +000039extern int do_reset (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]);
40
wdenk4e5ca3e2003-12-08 01:34:36 +000041static int linux_argc;
42static char **linux_argv;
43
44static char **linux_env;
45static char *linux_env_p;
46static int linux_env_idx;
47
48static void linux_params_init (ulong start, char *commandline);
49static void linux_env_set (char *env_name, char *env_val);
50
wdenk4e5ca3e2003-12-08 01:34:36 +000051void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
52 ulong addr, ulong * len_ptr, int verify)
53{
wdenk4e5ca3e2003-12-08 01:34:36 +000054 ulong len = 0, checksum;
55 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];
61
62 theKernel =
63 (void (*)(int, char **, char **, int *)) ntohl (hdr->ih_ep);
64
65 /*
66 * Check if there is an initrd image
67 */
68 if (argc >= 3) {
Heiko Schocherfad63402007-07-13 09:54:17 +020069 show_boot_progress (9);
wdenk4e5ca3e2003-12-08 01:34:36 +000070
71 addr = simple_strtoul (argv[2], NULL, 16);
72
73 printf ("## Loading Ramdisk Image at %08lx ...\n", addr);
74
75 /* Copy header so we can blank CRC field for re-calculation */
76 memcpy (&header, (char *) addr, sizeof (image_header_t));
77
78 if (ntohl (hdr->ih_magic) != IH_MAGIC) {
79 printf ("Bad Magic Number\n");
Heiko Schocherfad63402007-07-13 09:54:17 +020080 show_boot_progress (-10);
wdenk4e5ca3e2003-12-08 01:34:36 +000081 do_reset (cmdtp, flag, argc, argv);
82 }
83
84 data = (ulong) & header;
85 len = sizeof (image_header_t);
86
87 checksum = ntohl (hdr->ih_hcrc);
88 hdr->ih_hcrc = 0;
89
90 if (crc32 (0, (char *) data, len) != checksum) {
91 printf ("Bad Header Checksum\n");
Heiko Schocherfad63402007-07-13 09:54:17 +020092 show_boot_progress (-11);
wdenk4e5ca3e2003-12-08 01:34:36 +000093 do_reset (cmdtp, flag, argc, argv);
94 }
95
Heiko Schocherfad63402007-07-13 09:54:17 +020096 show_boot_progress (10);
wdenk4e5ca3e2003-12-08 01:34:36 +000097
98 print_image_hdr (hdr);
99
100 data = addr + sizeof (image_header_t);
101 len = ntohl (hdr->ih_size);
102
103 if (verify) {
104 ulong csum = 0;
105
106 printf (" Verifying Checksum ... ");
107 csum = crc32 (0, (char *) data, len);
108 if (csum != ntohl (hdr->ih_dcrc)) {
109 printf ("Bad Data CRC\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200110 show_boot_progress (-12);
wdenk4e5ca3e2003-12-08 01:34:36 +0000111 do_reset (cmdtp, flag, argc, argv);
112 }
113 printf ("OK\n");
114 }
115
Heiko Schocherfad63402007-07-13 09:54:17 +0200116 show_boot_progress (11);
wdenk4e5ca3e2003-12-08 01:34:36 +0000117
118 if ((hdr->ih_os != IH_OS_LINUX) ||
wdenkbf9e3b32004-02-12 00:47:09 +0000119 (hdr->ih_arch != IH_CPU_M68K) ||
wdenk4e5ca3e2003-12-08 01:34:36 +0000120 (hdr->ih_type != IH_TYPE_RAMDISK)) {
wdenkbf9e3b32004-02-12 00:47:09 +0000121 printf ("No Linux M68K Ramdisk Image\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200122 show_boot_progress (-13);
wdenk4e5ca3e2003-12-08 01:34:36 +0000123 do_reset (cmdtp, flag, argc, argv);
124 }
125
126 /*
127 * Now check if we have a multifile image
128 */
129 } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
130 ulong tail = ntohl (len_ptr[0]) % 4;
131 int i;
132
Heiko Schocherfad63402007-07-13 09:54:17 +0200133 show_boot_progress (13);
wdenk4e5ca3e2003-12-08 01:34:36 +0000134
135 /* skip kernel length and terminator */
136 data = (ulong) (&len_ptr[2]);
137 /* skip any additional image length fields */
138 for (i = 1; len_ptr[i]; ++i)
139 data += 4;
140 /* add kernel length, and align */
141 data += ntohl (len_ptr[0]);
142 if (tail) {
143 data += 4 - tail;
144 }
145
146 len = ntohl (len_ptr[1]);
147
148 } else {
149 /*
150 * no initrd image
151 */
Heiko Schocherfad63402007-07-13 09:54:17 +0200152 show_boot_progress (14);
wdenk4e5ca3e2003-12-08 01:34:36 +0000153
154 data = 0;
155 }
156
157#ifdef DEBUG
158 if (!data) {
159 printf ("No initrd\n");
160 }
161#endif
162
163 if (data) {
164 initrd_start = data;
165 initrd_end = initrd_start + len;
166 } else {
167 initrd_start = 0;
168 initrd_end = 0;
169 }
170
Heiko Schocherfad63402007-07-13 09:54:17 +0200171 show_boot_progress (15);
wdenk4e5ca3e2003-12-08 01:34:36 +0000172
173#ifdef DEBUG
174 printf ("## Transferring control to Linux (at address %08lx) ...\n",
175 (ulong) theKernel);
176#endif
177
178 linux_params_init (PHYSADDR (gd->bd->bi_boot_params), commandline);
179
180 sprintf (env_buf, "%lu", gd->ram_size >> 20);
181 linux_env_set ("memsize", env_buf);
182
183 sprintf (env_buf, "0x%08X", (uint) PHYSADDR (initrd_start));
184 linux_env_set ("initrd_start", env_buf);
185
186 sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
187 linux_env_set ("initrd_size", env_buf);
188
189 sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
190 linux_env_set ("flash_start", env_buf);
191
192 sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
193 linux_env_set ("flash_size", env_buf);
194
195 /* we assume that the kernel is in place */
196 printf ("\nStarting kernel ...\n\n");
197
198 theKernel (linux_argc, linux_argv, linux_env, 0);
199}
200
201static void linux_params_init (ulong start, char *line)
202{
203 char *next, *quote, *argp;
204
205 linux_argc = 1;
206 linux_argv = (char **) start;
207 linux_argv[0] = 0;
208 argp = (char *) (linux_argv + LINUX_MAX_ARGS);
209
210 next = line;
211
212 while (line && *line && linux_argc < LINUX_MAX_ARGS) {
213 quote = strchr (line, '"');
214 next = strchr (line, ' ');
215
216 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;
242 }
243
244 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;
248}
249
250static void linux_env_set (char *env_name, char *env_val)
251{
252 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
253 linux_env[linux_env_idx] = linux_env_p;
254
255 strcpy (linux_env_p, env_name);
256 linux_env_p += strlen (env_name);
257
258 strcpy (linux_env_p, "=");
259 linux_env_p += 1;
260
261 strcpy (linux_env_p, env_val);
262 linux_env_p += strlen (env_val);
263
264 linux_env_p++;
265 linux_env[++linux_env_idx] = 0;
266 }
267}