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[]);
47 extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
50 DECLARE_GLOBAL_DATA_PTR;
53 #endif /* USE_HOSTCC*/
57 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
59 int image_check_hcrc (image_header_t *hdr)
62 ulong len = image_get_header_size ();
63 image_header_t header;
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);
69 hcrc = crc32 (0, (unsigned char *)&header, len);
71 return (hcrc == image_get_hcrc (hdr));
74 int image_check_dcrc (image_header_t *hdr)
76 ulong data = image_get_data (hdr);
77 ulong len = image_get_data_size (hdr);
78 ulong dcrc = crc32 (0, (unsigned char *)data, len);
80 return (dcrc == image_get_dcrc (hdr));
84 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
87 ulong len = image_get_data_size (hdr);
88 ulong data = image_get_data (hdr);
90 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
92 ulong edata = cdata + len;
94 while (cdata < edata) {
95 ulong chunk = edata - cdata;
99 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
105 dcrc = crc32 (0, (unsigned char *)data, len);
108 return (dcrc == image_get_dcrc (hdr));
111 int getenv_verify (void)
113 char *s = getenv ("verify");
114 return (s && (*s == 'n')) ? 0 : 1;
117 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
119 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
121 size_t tail = (len > chunksz) ? chunksz : len;
123 memmove (to, from, tail);
128 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
129 memmove (to, from, len);
130 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
132 #endif /* USE_HOSTCC */
135 * image_multi_count - get component (sub-image) count
136 * @hdr: pointer to the header of the multi component image
138 * image_multi_count() returns number of components in a multi
141 * Note: no checking of the image type is done, caller must pass
142 * a valid multi component image.
145 * number of components
147 ulong image_multi_count (image_header_t *hdr)
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);
156 /* count non empty slots */
157 for (i = 0; size[i]; ++i)
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
170 * image_multi_getimg() returns size and data address for the requested
171 * component in a multi component image.
173 * Note: no checking of the image type is done, caller must pass
174 * a valid multi component image.
177 * data address and size of the component, if idx is valid
178 * 0 in data and len, if idx is out of range
180 void image_multi_getimg (image_header_t *hdr, ulong idx,
181 ulong *data, ulong *len)
185 ulong offset, tail, count, img_data;
187 /* get number of component */
188 count = image_multi_count (hdr);
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);
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);
203 /* go over all indices preceding requested component idx */
204 for (i = 0; i < idx; i++) {
205 /* add up i-th component size */
208 /* add up alignment for i-th component */
209 tail += (4 - size[i] % 4);
212 /* calculate idx-th component data address */
213 *data = img_data + offset + tail;
221 const char* image_get_os_name (uint8_t 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;
234 case IH_OS_ARTOS: name = "ARTOS"; break;
236 #ifdef CONFIG_LYNXKDI
237 case IH_OS_LYNXOS: name = "LynxOS"; break;
239 default: name = "Unknown OS"; break;
245 const char* image_get_arch_name (uint8_t 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;
274 const char* image_get_type_name (uint8_t 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;
293 const char* image_get_comp_name (uint8_t 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;
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
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
322 * If dataflash support is enabled routine checks for dataflash addresses
323 * and handles required dataflash reads.
326 * pointer to a ramdisk image header, if image was found and valid
327 * otherwise, board is reset
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)
333 image_header_t *rd_hdr;
335 show_boot_progress (9);
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 (),
346 rd_hdr = (image_header_t *)rd_addr;
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);
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);
360 show_boot_progress (10);
361 print_image_hdr (rd_hdr);
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));
369 read_dataflash (rd_addr + image_get_header_size (),
370 image_get_data_size (rd_hdr),
371 (char *)image_get_data (rd_hdr));
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);
385 show_boot_progress (11);
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);
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
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.
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
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)
427 ulong rd_data, rd_len;
428 image_header_t *rd_hdr;
432 * Look for a '-' which indicates to ignore the
435 if (strcmp(argv[2], "-") == 0) {
436 debug ("## Skipping init Ramdisk\n");
437 rd_len = rd_data = 0;
440 * Check if there is an initrd image at the
441 * address provided in the second bootm argument
443 rd_addr = simple_strtoul (argv[2], NULL, 16);
444 printf ("## Loading init Ramdisk Image at %08lx ...\n",
447 rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
448 rd_addr, arch, verify);
450 rd_data = image_get_data (rd_hdr);
451 rd_len = image_get_data_size (rd_hdr);
453 #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
455 *we need to copy the ramdisk to SRAM to let Linux boot
457 memmove ((void *)image_get_load (rd_hdr),
458 (uchar *)rd_data, rd_len);
460 rd_data = image_get_load (rd_hdr);
461 #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
464 } else if (image_check_type (hdr, IH_TYPE_MULTI)) {
466 * Now check if we have a multifile image
467 * Get second entry data start address and len
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);
477 show_boot_progress (14);
478 rd_len = rd_data = 0;
482 debug ("## No init Ramdisk\n");
487 *rd_end = rd_data + rd_len;
489 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
493 #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
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)
506 * ramdisk_high() takes a relocation hint from "initrd_high" environement
507 * variable and if requested ramdisk data is moved to a specified location.
510 * - initrd_start and initrd_end are set to final (after relocation) ramdisk
511 * start/end addresses if ramdisk image start and len were provided
512 * otherwise set initrd_start and initrd_end set to zeros
513 * - returns new allc_current, next free address below BOOTMAPSZ
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)
521 int initrd_copy_to_ram = 1;
522 ulong new_alloc_current = alloc_current;
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.
528 initrd_high = simple_strtoul (s, NULL, 16);
529 if (initrd_high == ~0)
530 initrd_copy_to_ram = 0;
532 /* not set, no restrictions to load high */
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);
542 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
543 initrd_high, initrd_copy_to_ram);
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;
551 new_alloc_current = alloc_current - rd_len;
552 *initrd_start = new_alloc_current;
553 *initrd_start &= ~(4096 - 1); /* align on page */
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.
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.
569 nsp -= 2048; /* just to be sure */
572 if (nsp > initrd_high) /* limit as specified */
576 nsp &= ~(4096 - 1); /* align on page */
578 if (nsp >= sp_limit) {
580 new_alloc_current = alloc_current;
584 show_boot_progress (12);
586 *initrd_end = *initrd_start + rd_len;
587 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
588 *initrd_start, *initrd_end);
590 memmove_wd((void *)*initrd_start,
591 (void *)rd_data, rd_len, CHUNKSZ);
599 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
600 *initrd_start, *initrd_end);
602 return new_alloc_current;
606 * get_boot_sp_limit - calculate stack pointer limit
607 * @sp: current stack pointer
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.
614 * stack pointer limit
616 ulong get_boot_sp_limit(ulong sp)
620 sp_limit -= 2048; /* just to be sure */
622 /* make sure sp_limit is within kernel mapped space */
623 if (sp_limit > CFG_BOOTMAPSZ)
624 sp_limit = CFG_BOOTMAPSZ;
631 * get_boot_cmdline - allocate and initialize kernel cmdline
632 * @alloc_current: current boot allocation address (counting down
634 * @cmd_start: pointer to a ulong variable, will hold cmdline start
635 * @cmd_end: pointer to a ulong variable, will hold cmdline end
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
643 * alloc_current after cmdline allocation
645 ulong get_boot_cmdline (ulong alloc_current, ulong *cmd_start, ulong *cmd_end)
650 cmdline = (char *)((alloc_current - CFG_BARGSIZE) & ~0xF);
652 if ((s = getenv("bootargs")) == NULL)
657 *cmd_start = (ulong) & cmdline[0];
658 *cmd_end = *cmd_start + strlen(cmdline);
660 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
662 return (ulong)cmdline;
666 * get_boot_kbd - allocate and initialize kernel copy of board info
667 * @alloc_current: current boot allocation address (counting down
669 * @kbd: double pointer to board info data
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.
676 * alloc_current after kbd allocation
678 ulong get_boot_kbd (ulong alloc_current, bd_t **kbd)
680 *kbd = (bd_t *) (((ulong)alloc_current - sizeof(bd_t)) & ~0xF);
683 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
685 #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
686 do_bdinfo(NULL, 0, 0, NULL);
691 #endif /* CONFIG_PPC || CONFIG_M68K */
693 #endif /* USE_HOSTCC */