2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
32 #ifdef CONFIG_SHOW_BOOT_PROGRESS
33 #include <status_led.h>
36 #ifdef CONFIG_HAS_DATAFLASH
37 #include <dataflash.h>
40 #ifdef CONFIG_LOGBUFFER
44 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
51 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
53 int image_check_hcrc (image_header_t *hdr)
56 ulong len = image_get_header_size ();
57 image_header_t header;
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);
63 hcrc = crc32 (0, (unsigned char *)&header, len);
65 return (hcrc == image_get_hcrc (hdr));
68 int image_check_dcrc (image_header_t *hdr)
70 ulong data = image_get_data (hdr);
71 ulong len = image_get_data_size (hdr);
72 ulong dcrc = crc32 (0, (unsigned char *)data, len);
74 return (dcrc == image_get_dcrc (hdr));
78 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
81 ulong len = image_get_data_size (hdr);
82 ulong data = image_get_data (hdr);
84 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
86 ulong edata = cdata + len;
88 while (cdata < edata) {
89 ulong chunk = edata - cdata;
93 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
99 dcrc = crc32 (0, (unsigned char *)data, len);
102 return (dcrc == image_get_dcrc (hdr));
105 int getenv_verify (void)
107 char *s = getenv ("verify");
108 return (s && (*s == 'n')) ? 0 : 1;
111 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
113 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
115 size_t tail = (len > chunksz) ? chunksz : len;
117 memmove (to, from, tail);
122 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
123 memmove (to, from, len);
124 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
126 #endif /* USE_HOSTCC */
129 * image_multi_count - get component (sub-image) count
130 * @hdr: pointer to the header of the multi component image
132 * image_multi_count() returns number of components in a multi
135 * Note: no checking of the image type is done, caller must pass
136 * a valid multi component image.
139 * number of components
141 ulong image_multi_count (image_header_t *hdr)
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);
150 /* count non empty slots */
151 for (i = 0; size[i]; ++i)
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
164 * image_multi_getimg() returns size and data address for the requested
165 * component in a multi component image.
167 * Note: no checking of the image type is done, caller must pass
168 * a valid multi component image.
171 * data address and size of the component, if idx is valid
172 * 0 in data and len, if idx is out of range
174 void image_multi_getimg (image_header_t *hdr, ulong idx,
175 ulong *data, ulong *len)
179 ulong offset, tail, count, img_data;
181 /* get number of component */
182 count = image_multi_count (hdr);
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);
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);
197 /* go over all indices preceding requested component idx */
198 for (i = 0; i < idx; i++) {
199 /* add up i-th component size */
202 /* add up alignment for i-th component */
203 tail += (4 - size[i] % 4);
206 /* calculate idx-th component data address */
207 *data = img_data + offset + tail;
215 const char* image_get_os_name (uint8_t 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;
228 case IH_OS_ARTOS: name = "ARTOS"; break;
230 #ifdef CONFIG_LYNXKDI
231 case IH_OS_LYNXOS: name = "LynxOS"; break;
233 default: name = "Unknown OS"; break;
239 const char* image_get_arch_name (uint8_t 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;
268 const char* image_get_type_name (uint8_t 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;
287 const char* image_get_comp_name (uint8_t 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;
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
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
316 * If dataflash support is enabled routine checks for dataflash addresses
317 * and handles required dataflash reads.
320 * pointer to a ramdisk image header, if image was found and valid
321 * otherwise, board is reset
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)
327 image_header_t *rd_hdr;
329 show_boot_progress (9);
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 (),
340 rd_hdr = (image_header_t *)rd_addr;
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);
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);
354 show_boot_progress (10);
355 print_image_hdr (rd_hdr);
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));
363 read_dataflash (rd_addr + image_get_header_size (),
364 image_get_data_size (rd_hdr),
365 (char *)image_get_data (rd_hdr));
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);
379 show_boot_progress (11);
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);
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
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.
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
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)
421 ulong rd_data, rd_len;
422 image_header_t *rd_hdr;
426 * Look for a '-' which indicates to ignore the
429 if (strcmp(argv[2], "-") == 0) {
430 debug ("## Skipping init Ramdisk\n");
431 rd_len = rd_data = 0;
434 * Check if there is an initrd image at the
435 * address provided in the second bootm argument
437 rd_addr = simple_strtoul (argv[2], NULL, 16);
438 printf ("## Loading init Ramdisk Image at %08lx ...\n",
441 rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
442 rd_addr, arch, verify);
444 rd_data = image_get_data (rd_hdr);
445 rd_len = image_get_data_size (rd_hdr);
447 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
449 *we need to copy the ramdisk to SRAM to let Linux boot
451 memmove ((void *)image_get_load (rd_hdr),
452 (uchar *)rd_data, rd_len);
454 rd_data = image_get_load (rd_hdr);
455 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
458 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
460 * Now check if we have a multifile image
461 * Get second entry data start address and len
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);
471 show_boot_progress (14);
472 rd_len = rd_data = 0;
476 debug ("## No init Ramdisk\n");
481 *rd_end = rd_data + rd_len;
483 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
487 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
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)
500 * ramdisk_high() takes a relocation hint from "initrd_high" environement
501 * variable and if requested ramdisk data is moved to a specified location.
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
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)
514 int initrd_copy_to_ram = 1;
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.
520 initrd_high = simple_strtoul (s, NULL, 16);
521 if (initrd_high == ~0)
522 initrd_copy_to_ram = 0;
524 /* not set, no restrictions to load high */
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);
534 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
535 initrd_high, initrd_copy_to_ram);
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;
543 *initrd_start = (ulong)kbd - rd_len;
544 *initrd_start &= ~(4096 - 1); /* align on page */
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.
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.
560 nsp -= 2048; /* just to be sure */
563 if (nsp > initrd_high) /* limit as specified */
567 nsp &= ~(4096 - 1); /* align on page */
573 show_boot_progress (12);
575 *initrd_end = *initrd_start + rd_len;
576 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
577 *initrd_start, *initrd_end);
579 memmove_wd((void *)*initrd_start,
580 (void *)rd_data, rd_len, CHUNKSZ);
588 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
589 *initrd_start, *initrd_end);
591 #endif /* CONFIG_PPC || CONFIG_M68K */
592 #endif /* USE_HOSTCC */