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 #ifdef CONFIG_SHOW_BOOT_PROGRESS
37 # include <status_led.h>
38 # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
40 # define SHOW_BOOT_PROGRESS(arg)
43 extern image_header_t header; /* from cmd_bootm.c */
45 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
47 static int linux_argc;
48 static char ** linux_argv;
50 static char ** linux_env;
51 static char * linux_env_p;
52 static int linux_env_idx;
54 static void linux_params_init (ulong start, char * commandline);
55 static void linux_env_set (char * env_name, char * env_val);
58 void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[],
59 ulong addr, ulong * len_ptr, int verify)
61 ulong len = 0, checksum;
62 ulong initrd_start, initrd_end;
64 void (*theKernel) (int, char **, char **, int *);
65 image_header_t *hdr = &header;
66 char *commandline = getenv ("bootargs");
70 (void (*)(int, char **, char **, int *)) ntohl (hdr->ih_ep);
73 * Check if there is an initrd image
76 SHOW_BOOT_PROGRESS (9);
78 addr = simple_strtoul (argv[2], NULL, 16);
80 printf ("## Loading Ramdisk Image at %08lx ...\n", addr);
82 /* Copy header so we can blank CRC field for re-calculation */
83 memcpy (&header, (char *) addr, sizeof (image_header_t));
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);
91 data = (ulong) & header;
92 len = sizeof (image_header_t);
94 checksum = ntohl (hdr->ih_hcrc);
97 if (crc32 (0, (uchar *) data, len) != checksum) {
98 printf ("Bad Header Checksum\n");
99 SHOW_BOOT_PROGRESS (-11);
100 do_reset (cmdtp, flag, argc, argv);
103 SHOW_BOOT_PROGRESS (10);
105 print_image_hdr (hdr);
107 data = addr + sizeof (image_header_t);
108 len = ntohl (hdr->ih_size);
113 printf (" Verifying Checksum ... ");
114 csum = crc32 (0, (uchar *) 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);
123 SHOW_BOOT_PROGRESS (11);
125 if ((hdr->ih_os != IH_OS_LINUX) ||
126 (hdr->ih_arch != IH_CPU_MIPS) ||
127 (hdr->ih_type != IH_TYPE_RAMDISK)) {
128 printf ("No Linux MIPS Ramdisk Image\n");
129 SHOW_BOOT_PROGRESS (-13);
130 do_reset (cmdtp, flag, argc, argv);
134 * Now check if we have a multifile image
136 } else if ((hdr->ih_type == IH_TYPE_MULTI) && (len_ptr[1])) {
137 ulong tail = ntohl (len_ptr[0]) % 4;
140 SHOW_BOOT_PROGRESS (13);
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)
147 /* add kernel length, and align */
148 data += ntohl (len_ptr[0]);
153 len = ntohl (len_ptr[1]);
159 SHOW_BOOT_PROGRESS (14);
166 printf ("No initrd\n");
172 initrd_end = initrd_start + len;
178 SHOW_BOOT_PROGRESS (15);
181 printf ("## Transferring control to Linux (at address %08lx) ...\n",
185 linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline);
187 #ifdef CONFIG_MEMSIZE_IN_BYTES
188 sprintf (env_buf, "%lu", gd->ram_size);
190 printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size);
193 sprintf (env_buf, "%lu", gd->ram_size >> 20);
195 printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20);
197 #endif /* CONFIG_MEMSIZE_IN_BYTES */
199 linux_env_set ("memsize", env_buf);
201 sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start));
202 linux_env_set ("initrd_start", env_buf);
204 sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start));
205 linux_env_set ("initrd_size", env_buf);
207 sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart));
208 linux_env_set ("flash_start", env_buf);
210 sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize));
211 linux_env_set ("flash_size", env_buf);
213 /* we assume that the kernel is in place */
214 printf ("\nStarting kernel ...\n\n");
216 theKernel (linux_argc, linux_argv, linux_env, 0);
219 static void linux_params_init (ulong start, char *line)
221 char *next, *quote, *argp;
224 linux_argv = (char **) start;
226 argp = (char *) (linux_argv + LINUX_MAX_ARGS);
230 while (line && *line && linux_argc < LINUX_MAX_ARGS) {
231 quote = strchr (line, '"');
232 next = strchr (line, ' ');
234 while (next != NULL && quote != NULL && quote < next) {
235 /* we found a left quote before the next blank
236 * now we have to find the matching right quote
238 next = strchr (quote + 1, '"');
240 quote = strchr (next + 1, '"');
241 next = strchr (next + 1, ' ');
246 next = line + strlen (line);
249 linux_argv[linux_argc] = argp;
250 memcpy (argp, line, next - line);
251 argp[next - line] = 0;
253 argp += next - line + 1;
262 linux_env = (char **) (((ulong) argp + 15) & ~15);
264 linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS);
268 static void linux_env_set (char *env_name, char *env_val)
270 if (linux_env_idx < LINUX_MAX_ENVS - 1) {
271 linux_env[linux_env_idx] = linux_env_p;
273 strcpy (linux_env_p, env_name);
274 linux_env_p += strlen (env_name);
276 strcpy (linux_env_p, "=");
279 strcpy (linux_env_p, env_val);
280 linux_env_p += strlen (env_val);
283 linux_env[++linux_env_idx] = 0;