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 | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 36 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
| 37 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 38 | static int linux_argc; |
| 39 | static char ** linux_argv; |
| 40 | |
| 41 | static char ** linux_env; |
| 42 | static char * linux_env_p; |
| 43 | static int linux_env_idx; |
| 44 | |
| 45 | static void linux_params_init (ulong start, char * commandline); |
| 46 | static void linux_env_set (char * env_name, char * env_val); |
| 47 | |
| 48 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 49 | void do_bootm_linux (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[], |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 50 | image_header_t *hdr, int verify) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 51 | { |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 52 | ulong rd_data, rd_len; |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 53 | ulong initrd_start, initrd_end; |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 54 | image_header_t *rd_hdr; |
| 55 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 56 | void (*theKernel) (int, char **, char **, int *); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 57 | char *commandline = getenv ("bootargs"); |
| 58 | char env_buf[12]; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 59 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 60 | theKernel = |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 61 | (void (*)(int, char **, char **, int *))image_get_ep (hdr); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 62 | |
| 63 | /* |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 64 | * Check if there is an initrd image |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 65 | */ |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 66 | if (argc >= 3) { |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 67 | show_boot_progress (9); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 68 | |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 69 | rd_hdr = (image_header_t *)simple_strtoul (argv[2], NULL, 16); |
| 70 | printf ("## Loading Ramdisk Image at %08lx ...\n", rd_hdr); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 71 | |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 72 | if (!image_check_magic (rd_hdr)) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 73 | printf ("Bad Magic Number\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 74 | show_boot_progress (-10); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 75 | do_reset (cmdtp, flag, argc, argv); |
| 76 | } |
| 77 | |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 78 | if (!image_check_hcrc (rd_hdr)) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 79 | printf ("Bad Header Checksum\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 80 | show_boot_progress (-11); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 81 | do_reset (cmdtp, flag, argc, argv); |
| 82 | } |
| 83 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 84 | show_boot_progress (10); |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 85 | print_image_hdr (rd_hdr); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 86 | |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 87 | rd_data = image_get_data (rd_hdr); |
| 88 | rd_len = image_get_data_size (rd_hdr); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 89 | |
| 90 | if (verify) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 91 | printf (" Verifying Checksum ... "); |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 92 | if (!image_check_dcrc (rd_hdr)) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 93 | printf ("Bad Data CRC\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 94 | show_boot_progress (-12); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 95 | do_reset (cmdtp, flag, argc, argv); |
| 96 | } |
| 97 | printf ("OK\n"); |
| 98 | } |
| 99 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 100 | show_boot_progress (11); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 101 | |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 102 | if (!image_check_os (rd_hdr, IH_OS_LINUX) || |
| 103 | !image_check_arch (rd_hdr, IH_ARCH_MIPS) || |
| 104 | !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 105 | printf ("No Linux MIPS Ramdisk Image\n"); |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 106 | show_boot_progress (-13); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 107 | do_reset (cmdtp, flag, argc, argv); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Now check if we have a multifile image |
| 112 | */ |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 113 | } else if (image_check_type (hdr, IH_TYPE_MULTI)) { |
| 114 | /* |
| 115 | * Get second entry data start address and len |
| 116 | */ |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 117 | show_boot_progress (13); |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 118 | image_multi_getimg (hdr, 1, &rd_data, &rd_len); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 119 | } else { |
| 120 | /* |
| 121 | * no initrd image |
| 122 | */ |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 123 | show_boot_progress (14); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 124 | |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 125 | rd_data = rd_len = 0; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 126 | } |
| 127 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 128 | #ifdef DEBUG |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 129 | if (!rd_data) { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 130 | printf ("No initrd\n"); |
| 131 | } |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame^] | 134 | if (rd_data) { |
| 135 | initrd_start = rd_data; |
| 136 | initrd_end = initrd_start + rd_len; |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 137 | } else { |
| 138 | initrd_start = 0; |
| 139 | initrd_end = 0; |
| 140 | } |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 141 | |
Heiko Schocher | fad6340 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 142 | show_boot_progress (15); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 143 | |
| 144 | #ifdef DEBUG |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 145 | printf ("## Transferring control to Linux (at address %08lx) ...\n", |
| 146 | (ulong) theKernel); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
wdenk | a2663ea | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 149 | linux_params_init (UNCACHED_SDRAM (gd->bd->bi_boot_params), commandline); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 150 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 151 | #ifdef CONFIG_MEMSIZE_IN_BYTES |
| 152 | sprintf (env_buf, "%lu", gd->ram_size); |
| 153 | #ifdef DEBUG |
| 154 | printf ("## Giving linux memsize in bytes, %lu\n", gd->ram_size); |
| 155 | #endif |
| 156 | #else |
| 157 | sprintf (env_buf, "%lu", gd->ram_size >> 20); |
| 158 | #ifdef DEBUG |
| 159 | printf ("## Giving linux memsize in MB, %lu\n", gd->ram_size >> 20); |
| 160 | #endif |
| 161 | #endif /* CONFIG_MEMSIZE_IN_BYTES */ |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 162 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 163 | linux_env_set ("memsize", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 164 | |
wdenk | a2663ea | 2003-12-07 18:32:37 +0000 | [diff] [blame] | 165 | sprintf (env_buf, "0x%08X", (uint) UNCACHED_SDRAM (initrd_start)); |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 166 | linux_env_set ("initrd_start", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 167 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 168 | sprintf (env_buf, "0x%X", (uint) (initrd_end - initrd_start)); |
| 169 | linux_env_set ("initrd_size", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 170 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 171 | sprintf (env_buf, "0x%08X", (uint) (gd->bd->bi_flashstart)); |
| 172 | linux_env_set ("flash_start", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 173 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 174 | sprintf (env_buf, "0x%X", (uint) (gd->bd->bi_flashsize)); |
| 175 | linux_env_set ("flash_size", env_buf); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 176 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 177 | /* we assume that the kernel is in place */ |
| 178 | printf ("\nStarting kernel ...\n\n"); |
| 179 | |
| 180 | theKernel (linux_argc, linux_argv, linux_env, 0); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 181 | } |
| 182 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 183 | static void linux_params_init (ulong start, char *line) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 184 | { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 185 | char *next, *quote, *argp; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 186 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 187 | linux_argc = 1; |
| 188 | linux_argv = (char **) start; |
| 189 | linux_argv[0] = 0; |
| 190 | argp = (char *) (linux_argv + LINUX_MAX_ARGS); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 191 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 192 | next = line; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 193 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 194 | while (line && *line && linux_argc < LINUX_MAX_ARGS) { |
| 195 | quote = strchr (line, '"'); |
| 196 | next = strchr (line, ' '); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 197 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 198 | while (next != NULL && quote != NULL && quote < next) { |
| 199 | /* we found a left quote before the next blank |
| 200 | * now we have to find the matching right quote |
| 201 | */ |
| 202 | next = strchr (quote + 1, '"'); |
| 203 | if (next != NULL) { |
| 204 | quote = strchr (next + 1, '"'); |
| 205 | next = strchr (next + 1, ' '); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (next == NULL) { |
| 210 | next = line + strlen (line); |
| 211 | } |
| 212 | |
| 213 | linux_argv[linux_argc] = argp; |
| 214 | memcpy (argp, line, next - line); |
| 215 | argp[next - line] = 0; |
| 216 | |
| 217 | argp += next - line + 1; |
| 218 | linux_argc++; |
| 219 | |
| 220 | if (*next) |
| 221 | next++; |
| 222 | |
| 223 | line = next; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 224 | } |
| 225 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 226 | linux_env = (char **) (((ulong) argp + 15) & ~15); |
| 227 | linux_env[0] = 0; |
| 228 | linux_env_p = (char *) (linux_env + LINUX_MAX_ENVS); |
| 229 | linux_env_idx = 0; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 230 | } |
| 231 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 232 | static void linux_env_set (char *env_name, char *env_val) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 233 | { |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 234 | if (linux_env_idx < LINUX_MAX_ENVS - 1) { |
| 235 | linux_env[linux_env_idx] = linux_env_p; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 236 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 237 | strcpy (linux_env_p, env_name); |
| 238 | linux_env_p += strlen (env_name); |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 239 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 240 | strcpy (linux_env_p, "="); |
| 241 | linux_env_p += 1; |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 242 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 243 | strcpy (linux_env_p, env_val); |
| 244 | linux_env_p += strlen (env_val); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 245 | |
wdenk | 5da627a | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 246 | linux_env_p++; |
| 247 | linux_env[++linux_env_idx] = 0; |
| 248 | } |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 249 | } |