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