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