wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 1 | /* |
| 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> |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 26 | #include <image.h> |
| 27 | #include <zlib.h> |
| 28 | #include <asm/byteorder.h> |
| 29 | #include <asm/addrspace.h> |
| 30 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 33 | #define LINUX_MAX_ENVS 256 |
| 34 | #define LINUX_MAX_ARGS 256 |
| 35 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 36 | extern image_header_t header; /* from cmd_bootm.c */ |
| 37 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 38 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
| 39 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 40 | static int linux_argc; |
| 41 | static char ** linux_argv; |
| 42 | |
| 43 | static char ** linux_env; |
| 44 | static char * linux_env_p; |
| 45 | static int linux_env_idx; |
| 46 | |
| 47 | static void linux_params_init (ulong start, char * commandline); |
| 48 | static void linux_env_set (char * env_name, char * env_val); |
| 49 | |
| 50 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 51 | void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[], |
| 52 | ulong addr, ulong * len_ptr, int verify) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 53 | { |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 54 | ulong len = 0; |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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]; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 61 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 62 | theKernel = |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 63 | (void (*)(int, char **, char **, int *))image_get_ep (hdr); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 66 | * Check if there is an initrd image |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 67 | */ |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 68 | if (argc >= 3) { |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 69 | show_boot_progress (9); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 70 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 71 | addr = simple_strtoul (argv[2], NULL, 16); |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 72 | hdr = (image_header_t *)addr; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 73 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 74 | printf ("## Loading Ramdisk Image at %08lx ...\n", addr); |
| 75 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 76 | if (!image_check_magic (hdr)) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 77 | printf ("Bad Magic Number\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 78 | show_boot_progress (-10); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 79 | do_reset (cmdtp, flag, argc, argv); |
| 80 | } |
| 81 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 82 | if (!image_check_hcrc (hdr)) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 83 | printf ("Bad Header Checksum\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 84 | show_boot_progress (-11); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 85 | do_reset (cmdtp, flag, argc, argv); |
| 86 | } |
| 87 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 88 | show_boot_progress (10); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 89 | |
| 90 | print_image_hdr (hdr); |
| 91 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 92 | data = image_get_data (hdr); |
| 93 | len = image_get_data_size (hdr); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 94 | |
| 95 | if (verify) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 96 | printf (" Verifying Checksum ... "); |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 97 | if (!image_check_dcrc (hdr)) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 98 | printf ("Bad Data CRC\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 99 | show_boot_progress (-12); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 100 | do_reset (cmdtp, flag, argc, argv); |
| 101 | } |
| 102 | printf ("OK\n"); |
| 103 | } |
| 104 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 105 | show_boot_progress (11); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 106 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 107 | if (!image_check_os (hdr, IH_OS_LINUX) || |
| 108 | !image_check_arch (hdr, IH_ARCH_MIPS) || |
| 109 | !image_check_type (hdr, IH_TYPE_RAMDISK)) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 110 | printf ("No Linux MIPS Ramdisk Image\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 111 | show_boot_progress (-13); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 112 | do_reset (cmdtp, flag, argc, argv); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Now check if we have a multifile image |
| 117 | */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 118 | } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) { |
| 119 | ulong tail = image_to_cpu (len_ptr[0]) % 4; |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 120 | int i; |
| 121 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 122 | show_boot_progress (13); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 123 | |
| 124 | /* skip kernel length and terminator */ |
| 125 | data = (ulong) (&len_ptr[2]); |
| 126 | /* skip any additional image length fields */ |
| 127 | for (i = 1; len_ptr[i]; ++i) |
| 128 | data += 4; |
| 129 | /* add kernel length, and align */ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 130 | data += image_to_cpu (len_ptr[0]); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 131 | if (tail) { |
| 132 | data += 4 - tail; |
| 133 | } |
| 134 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 135 | len = image_to_cpu (len_ptr[1]); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 136 | |
| 137 | } else { |
| 138 | /* |
| 139 | * no initrd image |
| 140 | */ |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 141 | show_boot_progress (14); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 142 | |
| 143 | data = 0; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 144 | } |
| 145 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 146 | #ifdef DEBUG |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 147 | if (!data) { |
| 148 | printf ("No initrd\n"); |
| 149 | } |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 152 | if (data) { |
| 153 | initrd_start = data; |
| 154 | initrd_end = initrd_start + len; |
| 155 | } else { |
| 156 | initrd_start = 0; |
| 157 | initrd_end = 0; |
| 158 | } |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 159 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 160 | show_boot_progress (15); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 161 | |
| 162 | #ifdef DEBUG |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 163 | printf ("## Transferring control to Linux (at address %08lx) ...\n", |
| 164 | (ulong) theKernel); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
wdenk | a2663ea | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 167 | linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 168 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 169 | #ifdef CONFIG_MEMSIZE_IN_BYTES |
| 170 | sprintf (env_buf, "%lu", gd->ram_size); |
| 171 | #ifdef DEBUG |
| 172 | printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size); |
| 173 | #endif |
| 174 | #else |
| 175 | sprintf (env_buf, "%lu", gd->ram_size >> 20); |
| 176 | #ifdef DEBUG |
| 177 | printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20); |
| 178 | #endif |
| 179 | #endif /* CONFIG_MEMSIZE_IN_BYTES */ |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 180 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 181 | linux_env_set ("memsize", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 182 | |
wdenk | a2663ea | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 183 | sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start)); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 184 | linux_env_set ("initrd_start", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 185 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 186 | sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start)); |
| 187 | linux_env_set ("initrd_size", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 188 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 189 | sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart)); |
| 190 | linux_env_set ("flash_start", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 191 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 192 | sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize)); |
| 193 | linux_env_set ("flash_size", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 194 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 199 | } |
| 200 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 201 | static void linux_params_init (ulong start, char *line) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 202 | { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 203 | char *next, *quote, *argp; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 204 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 205 | linux_argc = 1; |
| 206 | linux_argv = (char **) start; |
| 207 | linux_argv[0] = 0; |
| 208 | argp = (char *) (linux_argv + LINUX_MAX_ARGS); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 209 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 210 | next = line; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 211 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 212 | while (line && *line && linux_argc < LINUX_MAX_ARGS) { |
| 213 | quote = strchr (line, '"'); |
| 214 | next = strchr (line, ' '); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 215 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 242 | } |
| 243 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 248 | } |
| 249 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 250 | static void linux_env_set (char *env_name, char *env_val) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 251 | { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 252 | if (linux_env_idx < LINUX_MAX_ENVS - 1) { |
| 253 | linux_env[linux_env_idx] = linux_env_p; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 254 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 255 | strcpy (linux_env_p, env_name); |
| 256 | linux_env_p += strlen (env_name); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 257 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 258 | strcpy (linux_env_p, "="); |
| 259 | linux_env_p += 1; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 260 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 261 | strcpy (linux_env_p, env_val); |
| 262 | linux_env_p += strlen (env_val); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 263 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 264 | linux_env_p++; |
| 265 | linux_env[++linux_env_idx] = 0; |
| 266 | } |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 267 | } |