wdenk | bb1b826 | 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 | bb1b826 | 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 | |
| 31 | #define LINUX_MAX_ENVS 256 |
| 32 | #define LINUX_MAX_ARGS 256 |
| 33 | |
| 34 | #ifdef CONFIG_SHOW_BOOT_PROGRESS |
| 35 | # include <status_led.h> |
| 36 | # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg) |
| 37 | #else |
| 38 | # define SHOW_BOOT_PROGRESS(arg) |
| 39 | #endif |
| 40 | |
| 41 | extern image_header_t header; /* from cmd_bootm.c */ |
| 42 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 43 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
| 44 | |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 45 | static int linux_argc; |
| 46 | static char ** linux_argv; |
| 47 | |
| 48 | static char ** linux_env; |
| 49 | static char * linux_env_p; |
| 50 | static int linux_env_idx; |
| 51 | |
| 52 | static void linux_params_init (ulong start, char * commandline); |
| 53 | static void linux_env_set (char * env_name, char * env_val); |
| 54 | |
| 55 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 56 | void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[], |
| 57 | ulong addr, ulong * len_ptr, int verify) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 58 | { |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 59 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 60 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 61 | 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]; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 68 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 69 | theKernel = |
| 70 | (void (*)(int, char **, char **, int *)) ntohl (hdr->ih_ep); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 71 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 72 | /* |
| 73 | * Check if there is an initrd image |
| 74 | */ |
| 75 | if (argc >= 3) { |
| 76 | SHOW_BOOT_PROGRESS (9); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 77 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 78 | addr = simple_strtoul (argv[2], NULL, 16); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 79 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 80 | printf ("## Loading Ramdisk Image at %08lx ...\n", addr); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 81 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 82 | /* Copy header so we can blank CRC field for re-calculation */ |
| 83 | memcpy (&header, (char *) addr, sizeof (image_header_t)); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 84 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 90 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 91 | data = (ulong) & header; |
| 92 | len = sizeof (image_header_t); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 93 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 94 | checksum = ntohl (hdr->ih_hcrc); |
| 95 | hdr->ih_hcrc = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 96 | |
Wolfgang Denk | f6a692b | 2005-12-04 00:40:34 +0100 | [diff] [blame] | 97 | if (crc32 (0, (uchar *) data, len) != checksum) { |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 98 | printf ("Bad Header Checksum\n"); |
| 99 | SHOW_BOOT_PROGRESS (-11); |
| 100 | do_reset (cmdtp, flag, argc, argv); |
| 101 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 102 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 103 | SHOW_BOOT_PROGRESS (10); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 104 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 105 | print_image_hdr (hdr); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 106 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 107 | data = addr + sizeof (image_header_t); |
| 108 | len = ntohl (hdr->ih_size); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 109 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 110 | if (verify) { |
| 111 | ulong csum = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 112 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 113 | printf (" Verifying Checksum ... "); |
Wolfgang Denk | f6a692b | 2005-12-04 00:40:34 +0100 | [diff] [blame] | 114 | csum = crc32 (0, (uchar *) data, len); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 122 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 123 | SHOW_BOOT_PROGRESS (11); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 124 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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); |
| 131 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 132 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 139 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 140 | SHOW_BOOT_PROGRESS (13); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 141 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 152 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 153 | len = ntohl (len_ptr[1]); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 154 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 155 | } else { |
| 156 | /* |
| 157 | * no initrd image |
| 158 | */ |
| 159 | SHOW_BOOT_PROGRESS (14); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 160 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 161 | data = 0; |
| 162 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 163 | |
| 164 | #ifdef DEBUG |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 165 | if (!data) { |
| 166 | printf ("No initrd\n"); |
| 167 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 168 | #endif |
| 169 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 170 | if (data) { |
| 171 | initrd_start = data; |
| 172 | initrd_end = initrd_start + len; |
| 173 | } else { |
| 174 | initrd_start = 0; |
| 175 | initrd_end = 0; |
| 176 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 177 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 178 | SHOW_BOOT_PROGRESS (15); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 179 | |
| 180 | #ifdef DEBUG |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 181 | printf ("## Transferring control to Linux (at address %08lx) ...\n", |
| 182 | (ulong) theKernel); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 183 | #endif |
| 184 | |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 185 | linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 186 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 187 | #ifdef CONFIG_MEMSIZE_IN_BYTES |
| 188 | sprintf (env_buf, "%lu", gd->ram_size); |
| 189 | #ifdef DEBUG |
| 190 | printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size); |
| 191 | #endif |
| 192 | #else |
| 193 | sprintf (env_buf, "%lu", gd->ram_size >> 20); |
| 194 | #ifdef DEBUG |
| 195 | printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20); |
| 196 | #endif |
| 197 | #endif /* CONFIG_MEMSIZE_IN_BYTES */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 198 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 199 | linux_env_set ("memsize", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 200 | |
wdenk | 4ea537d | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 201 | sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start)); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 202 | linux_env_set ("initrd_start", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 203 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 204 | sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start)); |
| 205 | linux_env_set ("initrd_size", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 206 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 207 | sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart)); |
| 208 | linux_env_set ("flash_start", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 209 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 210 | sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize)); |
| 211 | linux_env_set ("flash_size", env_buf); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 212 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 213 | /* we assume that the kernel is in place */ |
| 214 | printf ("\nStarting kernel ...\n\n"); |
| 215 | |
| 216 | theKernel (linux_argc, linux_argv, linux_env, 0); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 217 | } |
| 218 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 219 | static void linux_params_init (ulong start, char *line) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 220 | { |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 221 | char *next, *quote, *argp; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 222 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 223 | linux_argc = 1; |
| 224 | linux_argv = (char **) start; |
| 225 | linux_argv[0] = 0; |
| 226 | argp = (char *) (linux_argv + LINUX_MAX_ARGS); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 227 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 228 | next = line; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 229 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 230 | while (line && *line && linux_argc < LINUX_MAX_ARGS) { |
| 231 | quote = strchr (line, '"'); |
| 232 | next = strchr (line, ' '); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 233 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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 |
| 237 | */ |
| 238 | next = strchr (quote + 1, '"'); |
| 239 | if (next != NULL) { |
| 240 | quote = strchr (next + 1, '"'); |
| 241 | next = strchr (next + 1, ' '); |
| 242 | } |
| 243 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 244 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 245 | if (next == NULL) { |
| 246 | next = line + strlen (line); |
| 247 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 248 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 249 | linux_argv[linux_argc] = argp; |
| 250 | memcpy (argp, line, next - line); |
| 251 | argp[next - line] = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 252 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 253 | argp += next - line + 1; |
| 254 | linux_argc++; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 255 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 256 | if (*next) |
| 257 | next++; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 258 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 259 | line = next; |
| 260 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 261 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 262 | linux_env = (char **) (((ulong) argp + 15) & ~15); |
| 263 | linux_env[0] = 0; |
| 264 | linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS); |
| 265 | linux_env_idx = 0; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 266 | } |
| 267 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 268 | static void linux_env_set (char *env_name, char *env_val) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 269 | { |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 270 | if (linux_env_idx < LINUX_MAX_ENVS - 1) { |
| 271 | linux_env[linux_env_idx] = linux_env_p; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 272 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 273 | strcpy (linux_env_p, env_name); |
| 274 | linux_env_p += strlen (env_name); |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 275 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 276 | strcpy (linux_env_p, "="); |
| 277 | linux_env_p += 1; |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 278 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 279 | strcpy (linux_env_p, env_val); |
| 280 | linux_env_p += strlen (env_val); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 281 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 282 | linux_env_p++; |
| 283 | linux_env[++linux_env_idx] = 0; |
| 284 | } |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 285 | } |