3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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
28 #include <asm/byteorder.h>
29 #include <asm/addrspace.h>
31 DECLARE_GLOBAL_DATA_PTR;
33 #define LINUX_MAX_ENVS 256
34 #define LINUX_MAX_ARGS 256
36 extern image_header_t header; /* from cmd_bootm.c */
38 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
40 static int linux_argc;
41 static char ** linux_argv;
43 static char ** linux_env;
44 static char * linux_env_p;
45 static int linux_env_idx;
47 static void linux_params_init (ulong start, char * commandline);
48 static void linux_env_set (char * env_name, char * env_val);
51 void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
52 ulong addr, ulong * len_ptr, int verify)
54 ulong len = 0, checksum;
55 ulong initrd_start, initrd_end;
57 void (*theKernel) (int, char **, char **, int *);
58 image_header_t *hdr = &header;
59 char *commandline = getenv ("bootargs");
63 (void (*)(int, char **, char **, int *)) ntohl (hdr->ih_ep);
66 * Check if there is an initrd image
69 show_boot_progress (9);
71 addr = simple_strtoul (argv[2], NULL, 16);
73 printf ("## Loading Ramdisk Image at %08lx ...\n", addr);
75 /* Copy header so we can blank CRC field for re-calculation */
76 memcpy (&header, (char *) addr, sizeof (image_header_t));
78 if (ntohl (hdr->ih_magic) != IH_MAGIC) {
79 printf ("Bad Magic Number\n");
80 show_boot_progress (-10);
81 do_reset (cmdtp, flag, argc, argv);
84 data = (ulong) & header;
85 len = sizeof (image_header_t);
87 checksum = ntohl (hdr->ih_hcrc);
90 if (crc32 (0, (uchar *) data, len) != checksum) {
91 printf ("Bad Header Checksum\n");
92 show_boot_progress (-11);
93 do_reset (cmdtp, flag, argc, argv);
96 show_boot_progress (10);
98 print_image_hdr (hdr);
100 data = addr + sizeof (image_header_t);
101 len = ntohl (hdr->ih_size);
106 printf (" Verifying Checksum ... ");
107 csum = crc32 (0, (uchar *) data, len);
108 if (csum != ntohl (hdr->ih_dcrc)) {
109 printf ("Bad Data CRC\n");
110 show_boot_progress (-12);
111 do_reset (cmdtp, flag, argc, argv);
116 show_boot_progress (11);
118 if ((hdr->ih_os != IH_OS_LINUX) ||
119 (hdr->ih_arch != IH_CPU_MIPS) ||
120 (hdr->ih_type != IH_TYPE_RAMDISK)) {
121 printf ("No Linux MIPS Ramdisk Image\n");
122 show_boot_progress (-13);
123 do_reset (cmdtp, flag, argc, argv);
127 * Now check if we have a multifile image
129 } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
130 ulong tail = ntohl (len_ptr[0]) % 4;
133 show_boot_progress (13);
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)
140 /* add kernel length, and align */
141 data += ntohl (len_ptr[0]);
146 len = ntohl (len_ptr[1]);
152 show_boot_progress (14);
159 printf ("No initrd\n");
165 initrd_end = initrd_start + len;
171 show_boot_progress (15);
174 printf ("## Transferring control to Linux (at address %08lx) ...\n",
178 linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline);
180 #ifdef CONFIG_MEMSIZE_IN_BYTES
181 sprintf (env_buf, "%lu", gd->ram_size);
183 printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size);
186 sprintf (env_buf, "%lu", gd->ram_size >> 20);
188 printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20);
190 #endif /* CONFIG_MEMSIZE_IN_BYTES */
192 linux_env_set ("memsize", env_buf);
194 sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
195 linux_env_set ("initrd_start", env_buf);
197 sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
198 linux_env_set ("initrd_size", env_buf);
200 sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
201 linux_env_set ("flash_start", env_buf);
203 sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
204 linux_env_set ("flash_size", env_buf);
206 /* we assume that the kernel is in place */
207 printf ("\nStarting kernel ...\n\n");
209 theKernel (linux_argc, linux_argv, linux_env, 0);
212 static void linux_params_init (ulong start, char *line)
214 char *next, *quote, *argp;
217 linux_argv = (char **) start;
219 argp = (char *) (linux_argv + LINUX_MAX_ARGS);
223 while (line && *line && linux_argc < LINUX_MAX_ARGS) {
224 quote = strchr (line, '"');
225 next = strchr (line, ' ');
227 while (next != NULL && quote != NULL && quote < next) {
228 /* we found a left quote before the next blank
229 * now we have to find the matching right quote
231 next = strchr (quote + 1, '"');
233 quote = strchr (next + 1, '"');
234 next = strchr (next + 1, ' ');
239 next = line + strlen (line);
242 linux_argv[linux_argc] = argp;
243 memcpy (argp, line, next - line);
244 argp[next - line] = 0;
246 argp += next - line + 1;
255 linux_env = (char **) (((ulong) argp + 15) & ~15);
257 linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
261 static void linux_env_set (char *env_name, char *env_val)
263 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
264 linux_env[linux_env_idx] = linux_env_p;
266 strcpy (linux_env_p, env_name);
267 linux_env_p += strlen (env_name);
269 strcpy (linux_env_p, "=");
272 strcpy (linux_env_p, env_val);
273 linux_env_p += strlen (env_val);
276 linux_env[++linux_env_idx] = 0;