Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2008 Semihalf |
| 3 | * |
| 4 | * (C) Copyright 2000-2006 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 25 | |
| 26 | #define DEBUG |
| 27 | |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 28 | #ifndef USE_HOSTCC |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 29 | #include <common.h> |
| 30 | #include <watchdog.h> |
| 31 | |
| 32 | #ifdef CONFIG_SHOW_BOOT_PROGRESS |
| 33 | #include <status_led.h> |
| 34 | #endif |
| 35 | |
| 36 | #ifdef CONFIG_HAS_DATAFLASH |
| 37 | #include <dataflash.h> |
| 38 | #endif |
| 39 | |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 40 | #ifdef CONFIG_LOGBUFFER |
| 41 | #include <logbuff.h> |
| 42 | #endif |
| 43 | |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 44 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 45 | |
| 46 | #ifdef CONFIG_CMD_BDI |
| 47 | extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); |
| 48 | #endif |
| 49 | |
| 50 | DECLARE_GLOBAL_DATA_PTR; |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 51 | #else |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 52 | #include "mkimage.h" |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 53 | #endif /* USE_HOSTCC*/ |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 54 | |
| 55 | #include <image.h> |
| 56 | |
| 57 | unsigned long crc32 (unsigned long, const unsigned char *, unsigned int); |
| 58 | |
| 59 | int image_check_hcrc (image_header_t *hdr) |
| 60 | { |
| 61 | ulong hcrc; |
| 62 | ulong len = image_get_header_size (); |
| 63 | image_header_t header; |
| 64 | |
| 65 | /* Copy header so we can blank CRC field for re-calculation */ |
| 66 | memmove (&header, (char *)hdr, image_get_header_size ()); |
| 67 | image_set_hcrc (&header, 0); |
| 68 | |
| 69 | hcrc = crc32 (0, (unsigned char *)&header, len); |
| 70 | |
| 71 | return (hcrc == image_get_hcrc (hdr)); |
| 72 | } |
| 73 | |
| 74 | int image_check_dcrc (image_header_t *hdr) |
| 75 | { |
| 76 | ulong data = image_get_data (hdr); |
| 77 | ulong len = image_get_data_size (hdr); |
| 78 | ulong dcrc = crc32 (0, (unsigned char *)data, len); |
| 79 | |
| 80 | return (dcrc == image_get_dcrc (hdr)); |
| 81 | } |
| 82 | |
Marian Balakowicz | af13cdb | 2008-01-08 18:11:45 +0100 | [diff] [blame] | 83 | #ifndef USE_HOSTCC |
Marian Balakowicz | b97a2a0 | 2008-01-08 18:14:09 +0100 | [diff] [blame] | 84 | int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz) |
| 85 | { |
| 86 | ulong dcrc = 0; |
| 87 | ulong len = image_get_data_size (hdr); |
| 88 | ulong data = image_get_data (hdr); |
| 89 | |
| 90 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
| 91 | ulong cdata = data; |
| 92 | ulong edata = cdata + len; |
| 93 | |
| 94 | while (cdata < edata) { |
| 95 | ulong chunk = edata - cdata; |
| 96 | |
| 97 | if (chunk > chunksz) |
| 98 | chunk = chunksz; |
| 99 | dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk); |
| 100 | cdata += chunk; |
| 101 | |
| 102 | WATCHDOG_RESET (); |
| 103 | } |
| 104 | #else |
| 105 | dcrc = crc32 (0, (unsigned char *)data, len); |
| 106 | #endif |
| 107 | |
| 108 | return (dcrc == image_get_dcrc (hdr)); |
| 109 | } |
| 110 | |
| 111 | int getenv_verify (void) |
| 112 | { |
| 113 | char *s = getenv ("verify"); |
| 114 | return (s && (*s == 'n')) ? 0 : 1; |
| 115 | } |
Marian Balakowicz | af13cdb | 2008-01-08 18:11:45 +0100 | [diff] [blame] | 116 | |
| 117 | void memmove_wd (void *to, void *from, size_t len, ulong chunksz) |
| 118 | { |
| 119 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
| 120 | while (len > 0) { |
| 121 | size_t tail = (len > chunksz) ? chunksz : len; |
| 122 | WATCHDOG_RESET (); |
| 123 | memmove (to, from, tail); |
| 124 | to += tail; |
| 125 | from += tail; |
| 126 | len -= tail; |
| 127 | } |
| 128 | #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ |
| 129 | memmove (to, from, len); |
| 130 | #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ |
| 131 | } |
| 132 | #endif /* USE_HOSTCC */ |
Marian Balakowicz | f13e7b2 | 2008-01-08 18:12:17 +0100 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * image_multi_count - get component (sub-image) count |
| 136 | * @hdr: pointer to the header of the multi component image |
| 137 | * |
| 138 | * image_multi_count() returns number of components in a multi |
| 139 | * component image. |
| 140 | * |
| 141 | * Note: no checking of the image type is done, caller must pass |
| 142 | * a valid multi component image. |
| 143 | * |
| 144 | * returns: |
| 145 | * number of components |
| 146 | */ |
| 147 | ulong image_multi_count (image_header_t *hdr) |
| 148 | { |
| 149 | ulong i, count = 0; |
| 150 | ulong *size; |
| 151 | |
| 152 | /* get start of the image payload, which in case of multi |
| 153 | * component images that points to a table of component sizes */ |
| 154 | size = (ulong *)image_get_data (hdr); |
| 155 | |
| 156 | /* count non empty slots */ |
| 157 | for (i = 0; size[i]; ++i) |
| 158 | count++; |
| 159 | |
| 160 | return count; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * image_multi_getimg - get component data address and size |
| 165 | * @hdr: pointer to the header of the multi component image |
| 166 | * @idx: index of the requested component |
| 167 | * @data: pointer to a ulong variable, will hold component data address |
| 168 | * @len: pointer to a ulong variable, will hold component size |
| 169 | * |
| 170 | * image_multi_getimg() returns size and data address for the requested |
| 171 | * component in a multi component image. |
| 172 | * |
| 173 | * Note: no checking of the image type is done, caller must pass |
| 174 | * a valid multi component image. |
| 175 | * |
| 176 | * returns: |
| 177 | * data address and size of the component, if idx is valid |
| 178 | * 0 in data and len, if idx is out of range |
| 179 | */ |
| 180 | void image_multi_getimg (image_header_t *hdr, ulong idx, |
| 181 | ulong *data, ulong *len) |
| 182 | { |
| 183 | int i; |
| 184 | ulong *size; |
| 185 | ulong offset, tail, count, img_data; |
| 186 | |
| 187 | /* get number of component */ |
| 188 | count = image_multi_count (hdr); |
| 189 | |
| 190 | /* get start of the image payload, which in case of multi |
| 191 | * component images that points to a table of component sizes */ |
| 192 | size = (ulong *)image_get_data (hdr); |
| 193 | |
| 194 | /* get address of the proper component data start, which means |
| 195 | * skipping sizes table (add 1 for last, null entry) */ |
| 196 | img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong); |
| 197 | |
| 198 | if (idx < count) { |
| 199 | *len = size[idx]; |
| 200 | offset = 0; |
| 201 | tail = 0; |
| 202 | |
| 203 | /* go over all indices preceding requested component idx */ |
| 204 | for (i = 0; i < idx; i++) { |
| 205 | /* add up i-th component size */ |
| 206 | offset += size[i]; |
| 207 | |
| 208 | /* add up alignment for i-th component */ |
| 209 | tail += (4 - size[i] % 4); |
| 210 | } |
| 211 | |
| 212 | /* calculate idx-th component data address */ |
| 213 | *data = img_data + offset + tail; |
| 214 | } else { |
| 215 | *len = 0; |
| 216 | *data = 0; |
| 217 | } |
| 218 | } |
Marian Balakowicz | 42b73e8 | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 219 | |
| 220 | #ifndef USE_HOSTCC |
| 221 | const char* image_get_os_name (uint8_t os) |
| 222 | { |
| 223 | const char *name; |
| 224 | |
| 225 | switch (os) { |
| 226 | case IH_OS_INVALID: name = "Invalid OS"; break; |
| 227 | case IH_OS_NETBSD: name = "NetBSD"; break; |
| 228 | case IH_OS_LINUX: name = "Linux"; break; |
| 229 | case IH_OS_VXWORKS: name = "VxWorks"; break; |
| 230 | case IH_OS_QNX: name = "QNX"; break; |
| 231 | case IH_OS_U_BOOT: name = "U-Boot"; break; |
| 232 | case IH_OS_RTEMS: name = "RTEMS"; break; |
| 233 | #ifdef CONFIG_ARTOS |
| 234 | case IH_OS_ARTOS: name = "ARTOS"; break; |
| 235 | #endif |
| 236 | #ifdef CONFIG_LYNXKDI |
| 237 | case IH_OS_LYNXOS: name = "LynxOS"; break; |
| 238 | #endif |
| 239 | default: name = "Unknown OS"; break; |
| 240 | } |
| 241 | |
| 242 | return name; |
| 243 | } |
| 244 | |
| 245 | const char* image_get_arch_name (uint8_t arch) |
| 246 | { |
| 247 | const char *name; |
| 248 | |
| 249 | switch (arch) { |
| 250 | case IH_ARCH_INVALID: name = "Invalid Architecture"; break; |
| 251 | case IH_ARCH_ALPHA: name = "Alpha"; break; |
| 252 | case IH_ARCH_ARM: name = "ARM"; break; |
| 253 | case IH_ARCH_AVR32: name = "AVR32"; break; |
| 254 | case IH_ARCH_BLACKFIN: name = "Blackfin"; break; |
| 255 | case IH_ARCH_I386: name = "Intel x86"; break; |
| 256 | case IH_ARCH_IA64: name = "IA64"; break; |
| 257 | case IH_ARCH_M68K: name = "M68K"; break; |
| 258 | case IH_ARCH_MICROBLAZE:name = "Microblaze"; break; |
| 259 | case IH_ARCH_MIPS64: name = "MIPS 64 Bit"; break; |
| 260 | case IH_ARCH_MIPS: name = "MIPS"; break; |
| 261 | case IH_ARCH_NIOS2: name = "Nios-II"; break; |
| 262 | case IH_ARCH_NIOS: name = "Nios"; break; |
| 263 | case IH_ARCH_PPC: name = "PowerPC"; break; |
| 264 | case IH_ARCH_S390: name = "IBM S390"; break; |
| 265 | case IH_ARCH_SH: name = "SuperH"; break; |
| 266 | case IH_ARCH_SPARC64: name = "SPARC 64 Bit"; break; |
| 267 | case IH_ARCH_SPARC: name = "SPARC"; break; |
| 268 | default: name = "Unknown Architecture"; break; |
| 269 | } |
| 270 | |
| 271 | return name; |
| 272 | } |
| 273 | |
| 274 | const char* image_get_type_name (uint8_t type) |
| 275 | { |
| 276 | const char *name; |
| 277 | |
| 278 | switch (type) { |
| 279 | case IH_TYPE_INVALID: name = "Invalid Image"; break; |
| 280 | case IH_TYPE_STANDALONE:name = "Standalone Program"; break; |
| 281 | case IH_TYPE_KERNEL: name = "Kernel Image"; break; |
| 282 | case IH_TYPE_RAMDISK: name = "RAMDisk Image"; break; |
| 283 | case IH_TYPE_MULTI: name = "Multi-File Image"; break; |
| 284 | case IH_TYPE_FIRMWARE: name = "Firmware"; break; |
| 285 | case IH_TYPE_SCRIPT: name = "Script"; break; |
| 286 | case IH_TYPE_FLATDT: name = "Flat Device Tree"; break; |
| 287 | default: name = "Unknown Image"; break; |
| 288 | } |
| 289 | |
| 290 | return name; |
| 291 | } |
| 292 | |
| 293 | const char* image_get_comp_name (uint8_t comp) |
| 294 | { |
| 295 | const char *name; |
| 296 | |
| 297 | switch (comp) { |
| 298 | case IH_COMP_NONE: name = "uncompressed"; break; |
| 299 | case IH_COMP_GZIP: name = "gzip compressed"; break; |
| 300 | case IH_COMP_BZIP2: name = "bzip2 compressed"; break; |
| 301 | default: name = "unknown compression"; break; |
| 302 | } |
| 303 | |
| 304 | return name; |
| 305 | } |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 306 | |
| 307 | /** |
| 308 | * image_get_ramdisk - get and verify ramdisk image |
| 309 | * @cmdtp: command table pointer |
| 310 | * @flag: command flag |
| 311 | * @argc: command argument count |
| 312 | * @argv: command argument list |
| 313 | * @rd_addr: ramdisk image start address |
| 314 | * @arch: expected ramdisk architecture |
| 315 | * @verify: checksum verification flag |
| 316 | * |
| 317 | * image_get_ramdisk() returns a pointer to the verified ramdisk image |
| 318 | * header. Routine receives image start address and expected architecture |
| 319 | * flag. Verification done covers data and header integrity and os/type/arch |
| 320 | * fields checking. |
| 321 | * |
| 322 | * If dataflash support is enabled routine checks for dataflash addresses |
| 323 | * and handles required dataflash reads. |
| 324 | * |
| 325 | * returns: |
| 326 | * pointer to a ramdisk image header, if image was found and valid |
| 327 | * otherwise, board is reset |
| 328 | */ |
| 329 | image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag, |
| 330 | int argc, char *argv[], |
| 331 | ulong rd_addr, uint8_t arch, int verify) |
| 332 | { |
| 333 | image_header_t *rd_hdr; |
| 334 | |
| 335 | show_boot_progress (9); |
| 336 | |
| 337 | #ifdef CONFIG_HAS_DATAFLASH |
| 338 | if (addr_dataflash (rd_addr)) { |
| 339 | rd_hdr = (image_header_t *)CFG_LOAD_ADDR; |
| 340 | debug (" Reading Ramdisk image header from dataflash address " |
| 341 | "%08lx to %08lx\n", rd_addr, (ulong)rd_hdr); |
| 342 | read_dataflash (rd_addr, image_get_header_size (), |
| 343 | (char *)rd_hdr); |
| 344 | } else |
Marian Balakowicz | 42b73e8 | 2008-01-31 13:20:07 +0100 | [diff] [blame] | 345 | #endif |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 346 | rd_hdr = (image_header_t *)rd_addr; |
| 347 | |
| 348 | if (!image_check_magic (rd_hdr)) { |
| 349 | puts ("Bad Magic Number\n"); |
| 350 | show_boot_progress (-10); |
| 351 | do_reset (cmdtp, flag, argc, argv); |
| 352 | } |
| 353 | |
| 354 | if (!image_check_hcrc (rd_hdr)) { |
| 355 | puts ("Bad Header Checksum\n"); |
| 356 | show_boot_progress (-11); |
| 357 | do_reset (cmdtp, flag, argc, argv); |
| 358 | } |
| 359 | |
| 360 | show_boot_progress (10); |
| 361 | print_image_hdr (rd_hdr); |
| 362 | |
| 363 | #ifdef CONFIG_HAS_DATAFLASH |
| 364 | if (addr_dataflash (rd_addr)) { |
| 365 | debug (" Reading Ramdisk image data from dataflash address " |
| 366 | "%08lx to %08lx\n", rd_addr + image_get_header_size, |
| 367 | (ulong)image_get_data (rd_hdr)); |
| 368 | |
| 369 | read_dataflash (rd_addr + image_get_header_size (), |
| 370 | image_get_data_size (rd_hdr), |
| 371 | (char *)image_get_data (rd_hdr)); |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | if (verify) { |
| 376 | puts(" Verifying Checksum ... "); |
| 377 | if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) { |
| 378 | puts ("Bad Data CRC\n"); |
| 379 | show_boot_progress (-12); |
| 380 | do_reset (cmdtp, flag, argc, argv); |
| 381 | } |
| 382 | puts("OK\n"); |
| 383 | } |
| 384 | |
| 385 | show_boot_progress (11); |
| 386 | |
| 387 | if (!image_check_os (rd_hdr, IH_OS_LINUX) || |
| 388 | !image_check_arch (rd_hdr, arch) || |
| 389 | !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) { |
| 390 | printf ("No Linux %s Ramdisk Image\n", |
| 391 | image_get_arch_name(arch)); |
| 392 | show_boot_progress (-13); |
| 393 | do_reset (cmdtp, flag, argc, argv); |
| 394 | } |
| 395 | |
| 396 | return rd_hdr; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * get_ramdisk - main ramdisk handling routine |
| 401 | * @cmdtp: command table pointer |
| 402 | * @flag: command flag |
| 403 | * @argc: command argument count |
| 404 | * @argv: command argument list |
| 405 | * @hdr: pointer to the posiibly multi componet kernel image |
| 406 | * @verify: checksum verification flag |
| 407 | * @arch: expected ramdisk architecture |
| 408 | * @rd_start: pointer to a ulong variable, will hold ramdisk start address |
| 409 | * @rd_end: pointer to a ulong variable, will hold ramdisk end |
| 410 | * |
| 411 | * get_ramdisk() is responsible for finding a valid ramdisk image. |
| 412 | * Curently supported are the following ramdisk sources: |
| 413 | * - multicomponent kernel/ramdisk image, |
| 414 | * - commandline provided address of decicated ramdisk image. |
| 415 | * |
| 416 | * returns: |
| 417 | * rd_start and rd_end are set to ramdisk start/end addresses if |
| 418 | * ramdisk image is found and valid |
| 419 | * rd_start and rd_end are set to 0 if no ramdisk exists |
| 420 | * board is reset if ramdisk image is found but corrupted |
| 421 | */ |
| 422 | void get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], |
| 423 | image_header_t *hdr, int verify, uint8_t arch, |
| 424 | ulong *rd_start, ulong *rd_end) |
| 425 | { |
| 426 | ulong rd_addr; |
| 427 | ulong rd_data, rd_len; |
| 428 | image_header_t *rd_hdr; |
| 429 | |
| 430 | if (argc >= 3) { |
| 431 | /* |
| 432 | * Look for a '-' which indicates to ignore the |
| 433 | * ramdisk argument |
| 434 | */ |
| 435 | if (strcmp(argv[2], "-") == 0) { |
| 436 | debug ("## Skipping init Ramdisk\n"); |
| 437 | rd_len = rd_data = 0; |
| 438 | } else { |
| 439 | /* |
| 440 | * Check if there is an initrd image at the |
| 441 | * address provided in the second bootm argument |
| 442 | */ |
| 443 | rd_addr = simple_strtoul (argv[2], NULL, 16); |
| 444 | printf ("## Loading init Ramdisk Image at %08lx ...\n", |
| 445 | rd_addr); |
| 446 | |
| 447 | rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv, |
| 448 | rd_addr, arch, verify); |
| 449 | |
| 450 | rd_data = image_get_data (rd_hdr); |
| 451 | rd_len = image_get_data_size (rd_hdr); |
| 452 | |
| 453 | #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO) |
| 454 | /* |
| 455 | *we need to copy the ramdisk to SRAM to let Linux boot |
| 456 | */ |
| 457 | memmove ((void *)image_get_load (rd_hdr), |
| 458 | (uchar *)rd_data, rd_len); |
| 459 | |
| 460 | rd_data = image_get_load (rd_hdr); |
| 461 | #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */ |
| 462 | } |
| 463 | |
| 464 | } else if (image_check_type (hdr, IH_TYPE_MULTI)) { |
| 465 | /* |
| 466 | * Now check if we have a multifile image |
| 467 | * Get second entry data start address and len |
| 468 | */ |
| 469 | show_boot_progress (13); |
| 470 | printf ("## Loading init Ramdisk from multi component " |
| 471 | "Image at %08lx ...\n", (ulong)hdr); |
| 472 | image_multi_getimg (hdr, 1, &rd_data, &rd_len); |
| 473 | } else { |
| 474 | /* |
| 475 | * no initrd image |
| 476 | */ |
| 477 | show_boot_progress (14); |
| 478 | rd_len = rd_data = 0; |
| 479 | } |
| 480 | |
| 481 | if (!rd_data) { |
| 482 | debug ("## No init Ramdisk\n"); |
| 483 | *rd_start = 0; |
| 484 | *rd_end = 0; |
| 485 | } else { |
| 486 | *rd_start = rd_data; |
| 487 | *rd_end = rd_data + rd_len; |
| 488 | } |
| 489 | debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n", |
| 490 | *rd_start, *rd_end); |
| 491 | } |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 492 | |
| 493 | #if defined(CONFIG_PPC) || defined(CONFIG_M68K) |
| 494 | /** |
| 495 | * ramdisk_high - relocate init ramdisk |
| 496 | * @rd_data: ramdisk data start address |
| 497 | * @rd_len: ramdisk data length |
| 498 | * @kbd: kernel board info copy (within BOOTMAPSZ boundary) |
| 499 | * @sp_limit: stack pointer limit (including BOOTMAPSZ) |
| 500 | * @sp: current stack pointer |
| 501 | * @initrd_start: pointer to a ulong variable, will hold final init ramdisk |
| 502 | * start address (after possible relocation) |
| 503 | * @initrd_end: pointer to a ulong variable, will hold final init ramdisk |
| 504 | * end address (after possible relocation) |
| 505 | * |
| 506 | * ramdisk_high() takes a relocation hint from "initrd_high" environement |
| 507 | * variable and if requested ramdisk data is moved to a specified location. |
| 508 | * |
| 509 | * returns: |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 510 | * - initrd_start and initrd_end are set to final (after relocation) ramdisk |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 511 | * start/end addresses if ramdisk image start and len were provided |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 512 | * otherwise set initrd_start and initrd_end set to zeros |
| 513 | * - returns new allc_current, next free address below BOOTMAPSZ |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 514 | */ |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 515 | ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len, |
| 516 | bd_t *kbd, ulong sp_limit, ulong sp, |
| 517 | ulong *initrd_start, ulong *initrd_end) |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 518 | { |
| 519 | char *s; |
| 520 | ulong initrd_high; |
| 521 | int initrd_copy_to_ram = 1; |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 522 | ulong new_alloc_current = alloc_current; |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 523 | |
| 524 | if ((s = getenv ("initrd_high")) != NULL) { |
| 525 | /* a value of "no" or a similar string will act like 0, |
| 526 | * turning the "load high" feature off. This is intentional. |
| 527 | */ |
| 528 | initrd_high = simple_strtoul (s, NULL, 16); |
| 529 | if (initrd_high == ~0) |
| 530 | initrd_copy_to_ram = 0; |
| 531 | } else { |
| 532 | /* not set, no restrictions to load high */ |
| 533 | initrd_high = ~0; |
| 534 | } |
| 535 | |
| 536 | #ifdef CONFIG_LOGBUFFER |
| 537 | /* Prevent initrd from overwriting logbuffer */ |
| 538 | if (initrd_high < (kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD)) |
| 539 | initrd_high = kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD; |
| 540 | debug ("## Logbuffer at 0x%08lx ", kbd->bi_memsize - LOGBUFF_LEN); |
| 541 | #endif |
| 542 | debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n", |
| 543 | initrd_high, initrd_copy_to_ram); |
| 544 | |
| 545 | if (rd_data) { |
| 546 | if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ |
| 547 | debug (" in-place initrd\n"); |
| 548 | *initrd_start = rd_data; |
| 549 | *initrd_end = rd_data + rd_len; |
| 550 | } else { |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 551 | new_alloc_current = alloc_current - rd_len; |
| 552 | *initrd_start = new_alloc_current; |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 553 | *initrd_start &= ~(4096 - 1); /* align on page */ |
| 554 | |
| 555 | if (initrd_high) { |
| 556 | ulong nsp; |
| 557 | |
| 558 | /* |
| 559 | * the inital ramdisk does not need to be within |
| 560 | * CFG_BOOTMAPSZ as it is not accessed until after |
| 561 | * the mm system is initialised. |
| 562 | * |
| 563 | * do the stack bottom calculation again and see if |
| 564 | * the initrd will fit just below the monitor stack |
| 565 | * bottom without overwriting the area allocated |
| 566 | * for command line args and board info. |
| 567 | */ |
| 568 | nsp = sp; |
| 569 | nsp -= 2048; /* just to be sure */ |
| 570 | nsp &= ~0xF; |
| 571 | |
| 572 | if (nsp > initrd_high) /* limit as specified */ |
| 573 | nsp = initrd_high; |
| 574 | |
| 575 | nsp -= rd_len; |
| 576 | nsp &= ~(4096 - 1); /* align on page */ |
| 577 | |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 578 | if (nsp >= sp_limit) { |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 579 | *initrd_start = nsp; |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 580 | new_alloc_current = alloc_current; |
| 581 | } |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | show_boot_progress (12); |
| 585 | |
| 586 | *initrd_end = *initrd_start + rd_len; |
| 587 | printf (" Loading Ramdisk to %08lx, end %08lx ... ", |
| 588 | *initrd_start, *initrd_end); |
| 589 | |
| 590 | memmove_wd((void *)*initrd_start, |
| 591 | (void *)rd_data, rd_len, CHUNKSZ); |
| 592 | |
| 593 | puts ("OK\n"); |
| 594 | } |
| 595 | } else { |
| 596 | *initrd_start = 0; |
| 597 | *initrd_end = 0; |
| 598 | } |
| 599 | debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n", |
| 600 | *initrd_start, *initrd_end); |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 601 | |
| 602 | return new_alloc_current; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * get_boot_sp_limit - calculate stack pointer limit |
| 607 | * @sp: current stack pointer |
| 608 | * |
| 609 | * get_boot_sp_limit() takes current stack pointer adrress and calculates |
| 610 | * stack pointer limit, below which kernel boot data (cmdline, board info, |
| 611 | * etc.) will be allocated. |
| 612 | * |
| 613 | * returns: |
| 614 | * stack pointer limit |
| 615 | */ |
| 616 | ulong get_boot_sp_limit(ulong sp) |
| 617 | { |
| 618 | ulong sp_limit = sp; |
| 619 | |
| 620 | sp_limit -= 2048; /* just to be sure */ |
| 621 | |
| 622 | /* make sure sp_limit is within kernel mapped space */ |
| 623 | if (sp_limit > CFG_BOOTMAPSZ) |
| 624 | sp_limit = CFG_BOOTMAPSZ; |
| 625 | sp_limit &= ~0xF; |
| 626 | |
| 627 | return sp_limit; |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * get_boot_cmdline - allocate and initialize kernel cmdline |
| 632 | * @alloc_current: current boot allocation address (counting down |
| 633 | * from sp_limit) |
| 634 | * @cmd_start: pointer to a ulong variable, will hold cmdline start |
| 635 | * @cmd_end: pointer to a ulong variable, will hold cmdline end |
| 636 | * |
| 637 | * get_boot_cmdline() allocates space for kernel command line below |
| 638 | * provided alloc_current address. If "bootargs" U-boot environemnt |
| 639 | * variable is present its contents is copied to allocated kernel |
| 640 | * command line. |
| 641 | * |
| 642 | * returns: |
| 643 | * alloc_current after cmdline allocation |
| 644 | */ |
| 645 | ulong get_boot_cmdline (ulong alloc_current, ulong *cmd_start, ulong *cmd_end) |
| 646 | { |
| 647 | char *cmdline; |
| 648 | char *s; |
| 649 | |
| 650 | cmdline = (char *)((alloc_current - CFG_BARGSIZE) & ~0xF); |
| 651 | |
| 652 | if ((s = getenv("bootargs")) == NULL) |
| 653 | s = ""; |
| 654 | |
| 655 | strcpy(cmdline, s); |
| 656 | |
| 657 | *cmd_start = (ulong) & cmdline[0]; |
| 658 | *cmd_end = *cmd_start + strlen(cmdline); |
| 659 | |
| 660 | debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end); |
| 661 | |
| 662 | return (ulong)cmdline; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * get_boot_kbd - allocate and initialize kernel copy of board info |
| 667 | * @alloc_current: current boot allocation address (counting down |
| 668 | * from sp_limit) |
| 669 | * @kbd: double pointer to board info data |
| 670 | * |
| 671 | * get_boot_kbd() - allocates space for kernel copy of board info data. |
| 672 | * Space is allocated below provided alloc_current address and kernel |
| 673 | * board info is initialized with the current u-boot board info data. |
| 674 | * |
| 675 | * returns: |
| 676 | * alloc_current after kbd allocation |
| 677 | */ |
| 678 | ulong get_boot_kbd (ulong alloc_current, bd_t **kbd) |
| 679 | { |
| 680 | *kbd = (bd_t *) (((ulong)alloc_current - sizeof(bd_t)) & ~0xF); |
| 681 | **kbd = *(gd->bd); |
| 682 | |
| 683 | debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd); |
| 684 | |
| 685 | #if defined(DEBUG) && defined(CONFIG_CMD_BDI) |
| 686 | do_bdinfo(NULL, 0, 0, NULL); |
| 687 | #endif |
| 688 | |
| 689 | return (ulong)*kbd; |
Marian Balakowicz | ceaed2b | 2008-01-31 13:57:17 +0100 | [diff] [blame] | 690 | } |
| 691 | #endif /* CONFIG_PPC || CONFIG_M68K */ |
Marian Balakowicz | 5ad03eb | 2008-01-31 13:55:39 +0100 | [diff] [blame] | 692 | |
Marian Balakowicz | b6b0fe6 | 2008-01-31 13:58:13 +0100 | [diff] [blame^] | 693 | #endif /* USE_HOSTCC */ |