]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/image.c
[new uImage] Update naming convention for bootm/uImage related code
[people/ms/u-boot.git] / common / image.c
CommitLineData
b97a2a0a
MB
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 */
ceaed2b1
MB
25
26#define DEBUG
27
b97a2a0a 28#ifndef USE_HOSTCC
5ad03eb3
MB
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
ceaed2b1
MB
40#ifdef CONFIG_LOGBUFFER
41#include <logbuff.h>
42#endif
43
2242f536
MB
44#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
45#include <rtc.h>
46#endif
47
fff888a1
MB
48#if defined(CONFIG_FIT)
49#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
52#endif
53
b6b0fe64
MB
54#ifdef CONFIG_CMD_BDI
55extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
56#endif
57
58DECLARE_GLOBAL_DATA_PTR;
8a5ea3e6
MB
59
60static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
61 int argc, char *argv[],
62 ulong rd_addr, uint8_t arch, int verify);
b97a2a0a 63#else
5ad03eb3 64#include "mkimage.h"
b6b0fe64 65#endif /* USE_HOSTCC*/
b97a2a0a
MB
66
67#include <image.h>
68
69unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
70
9a4daad0
MB
71/*****************************************************************************/
72/* Legacy format routines */
73/*****************************************************************************/
b97a2a0a
MB
74int image_check_hcrc (image_header_t *hdr)
75{
76 ulong hcrc;
77 ulong len = image_get_header_size ();
78 image_header_t header;
79
80 /* Copy header so we can blank CRC field for re-calculation */
81 memmove (&header, (char *)hdr, image_get_header_size ());
82 image_set_hcrc (&header, 0);
83
84 hcrc = crc32 (0, (unsigned char *)&header, len);
85
86 return (hcrc == image_get_hcrc (hdr));
87}
88
89int image_check_dcrc (image_header_t *hdr)
90{
91 ulong data = image_get_data (hdr);
92 ulong len = image_get_data_size (hdr);
93 ulong dcrc = crc32 (0, (unsigned char *)data, len);
94
95 return (dcrc == image_get_dcrc (hdr));
96}
97
af13cdbc 98#ifndef USE_HOSTCC
b97a2a0a
MB
99int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
100{
101 ulong dcrc = 0;
102 ulong len = image_get_data_size (hdr);
103 ulong data = image_get_data (hdr);
104
105#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
106 ulong cdata = data;
107 ulong edata = cdata + len;
108
109 while (cdata < edata) {
110 ulong chunk = edata - cdata;
111
112 if (chunk > chunksz)
113 chunk = chunksz;
114 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
115 cdata += chunk;
116
117 WATCHDOG_RESET ();
118 }
119#else
120 dcrc = crc32 (0, (unsigned char *)data, len);
121#endif
122
123 return (dcrc == image_get_dcrc (hdr));
124}
125
f13e7b2e
MB
126/**
127 * image_multi_count - get component (sub-image) count
128 * @hdr: pointer to the header of the multi component image
129 *
130 * image_multi_count() returns number of components in a multi
131 * component image.
132 *
133 * Note: no checking of the image type is done, caller must pass
134 * a valid multi component image.
135 *
136 * returns:
137 * number of components
138 */
139ulong image_multi_count (image_header_t *hdr)
140{
141 ulong i, count = 0;
142 ulong *size;
143
144 /* get start of the image payload, which in case of multi
145 * component images that points to a table of component sizes */
146 size = (ulong *)image_get_data (hdr);
147
148 /* count non empty slots */
149 for (i = 0; size[i]; ++i)
150 count++;
151
152 return count;
153}
154
155/**
156 * image_multi_getimg - get component data address and size
157 * @hdr: pointer to the header of the multi component image
158 * @idx: index of the requested component
159 * @data: pointer to a ulong variable, will hold component data address
160 * @len: pointer to a ulong variable, will hold component size
161 *
162 * image_multi_getimg() returns size and data address for the requested
163 * component in a multi component image.
164 *
165 * Note: no checking of the image type is done, caller must pass
166 * a valid multi component image.
167 *
168 * returns:
169 * data address and size of the component, if idx is valid
170 * 0 in data and len, if idx is out of range
171 */
172void image_multi_getimg (image_header_t *hdr, ulong idx,
173 ulong *data, ulong *len)
174{
175 int i;
176 ulong *size;
177 ulong offset, tail, count, img_data;
178
179 /* get number of component */
180 count = image_multi_count (hdr);
181
182 /* get start of the image payload, which in case of multi
183 * component images that points to a table of component sizes */
184 size = (ulong *)image_get_data (hdr);
185
186 /* get address of the proper component data start, which means
187 * skipping sizes table (add 1 for last, null entry) */
188 img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong);
189
190 if (idx < count) {
191 *len = size[idx];
192 offset = 0;
193 tail = 0;
194
195 /* go over all indices preceding requested component idx */
196 for (i = 0; i < idx; i++) {
197 /* add up i-th component size */
198 offset += size[i];
199
200 /* add up alignment for i-th component */
201 tail += (4 - size[i] % 4);
202 }
203
204 /* calculate idx-th component data address */
205 *data = img_data + offset + tail;
206 } else {
207 *len = 0;
208 *data = 0;
209 }
210}
42b73e8e
MB
211
212#ifndef USE_HOSTCC
9a4daad0
MB
213static void image_print_type (image_header_t *hdr)
214{
215 const char *os, *arch, *type, *comp;
216
217 os = genimg_get_os_name (image_get_os (hdr));
218 arch = genimg_get_arch_name (image_get_arch (hdr));
219 type = genimg_get_type_name (image_get_type (hdr));
220 comp = genimg_get_comp_name (image_get_comp (hdr));
221
222 printf ("%s %s %s (%s)", arch, os, type, comp);
223}
224
225void image_print_contents (image_header_t *hdr)
226{
227#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
228 time_t timestamp = (time_t)image_get_time (hdr);
229 struct rtc_time tm;
230#endif
231
232 printf (" Image Name: %.*s\n", IH_NMLEN, image_get_name (hdr));
233
234#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
235 to_tm (timestamp, &tm);
236 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
237 tm.tm_year, tm.tm_mon, tm.tm_mday,
238 tm.tm_hour, tm.tm_min, tm.tm_sec);
239#endif
240 puts (" Image Type: ");
241 image_print_type (hdr);
242
243 printf ("\n Data Size: %d Bytes = ", image_get_data_size (hdr));
244 print_size (image_get_data_size (hdr), "\n");
245 printf (" Load Address: %08x\n"
246 " Entry Point: %08x\n",
247 image_get_load (hdr), image_get_ep (hdr));
248
249 if (image_check_type (hdr, IH_TYPE_MULTI)) {
250 int i;
251 ulong data, len;
252 ulong count = image_multi_count (hdr);
253
254 puts (" Contents:\n");
255 for (i = 0; i < count; i++) {
256 image_multi_getimg (hdr, i, &data, &len);
257 printf (" Image %d: %8ld Bytes = ", i, len);
258 print_size (len, "\n");
259 }
260 }
261}
262
263/**
264 * image_get_ramdisk - get and verify ramdisk image
265 * @cmdtp: command table pointer
266 * @flag: command flag
267 * @argc: command argument count
268 * @argv: command argument list
269 * @rd_addr: ramdisk image start address
270 * @arch: expected ramdisk architecture
271 * @verify: checksum verification flag
272 *
273 * image_get_ramdisk() returns a pointer to the verified ramdisk image
274 * header. Routine receives image start address and expected architecture
275 * flag. Verification done covers data and header integrity and os/type/arch
276 * fields checking.
277 *
278 * If dataflash support is enabled routine checks for dataflash addresses
279 * and handles required dataflash reads.
280 *
281 * returns:
282 * pointer to a ramdisk image header, if image was found and valid
283 * otherwise, return NULL
284 */
285static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
286 int argc, char *argv[],
287 ulong rd_addr, uint8_t arch, int verify)
288{
289 image_header_t *rd_hdr;
290
291 show_boot_progress (9);
292 rd_hdr = (image_header_t *)rd_addr;
293
294 if (!image_check_magic (rd_hdr)) {
295 puts ("Bad Magic Number\n");
296 show_boot_progress (-10);
297 return NULL;
298 }
299
300 if (!image_check_hcrc (rd_hdr)) {
301 puts ("Bad Header Checksum\n");
302 show_boot_progress (-11);
303 return NULL;
304 }
305
306 show_boot_progress (10);
307 image_print_contents (rd_hdr);
308
309 if (verify) {
310 puts(" Verifying Checksum ... ");
311 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
312 puts ("Bad Data CRC\n");
313 show_boot_progress (-12);
314 return NULL;
315 }
316 puts("OK\n");
317 }
318
319 show_boot_progress (11);
320
321 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
322 !image_check_arch (rd_hdr, arch) ||
323 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
324 printf ("No Linux %s Ramdisk Image\n",
325 genimg_get_arch_name(arch));
326 show_boot_progress (-13);
327 return NULL;
328 }
329
330 return rd_hdr;
331}
332
333/*****************************************************************************/
334/* Shared dual-format routines */
335/*****************************************************************************/
336int getenv_verify (void)
337{
338 char *s = getenv ("verify");
339 return (s && (*s == 'n')) ? 0 : 1;
340}
341
342int getenv_autostart (void)
343{
344 char *s = getenv ("autostart");
345 return (s && (*s == 'n')) ? 0 : 1;
346}
347
348ulong getenv_bootm_low(void)
349{
350 char *s = getenv ("bootm_low");
351 if (s) {
352 ulong tmp = simple_strtoul (s, NULL, 16);
353 return tmp;
354 }
355
356#ifdef CFG_SDRAM_BASE
357 return CFG_SDRAM_BASE;
358#else
359 return 0;
360#endif
361}
362
363ulong getenv_bootm_size(void)
364{
365 char *s = getenv ("bootm_size");
366 if (s) {
367 ulong tmp = simple_strtoul (s, NULL, 16);
368 return tmp;
369 }
370
371 return gd->bd->bi_memsize;
372}
373
374void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
375{
376#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
377 while (len > 0) {
378 size_t tail = (len > chunksz) ? chunksz : len;
379 WATCHDOG_RESET ();
380 memmove (to, from, tail);
381 to += tail;
382 from += tail;
383 len -= tail;
384 }
385#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
386 memmove (to, from, len);
387#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
388}
389#endif /* USE_HOSTCC */
390
391const char* genimg_get_os_name (uint8_t os)
42b73e8e
MB
392{
393 const char *name;
394
395 switch (os) {
396 case IH_OS_INVALID: name = "Invalid OS"; break;
397 case IH_OS_NETBSD: name = "NetBSD"; break;
398 case IH_OS_LINUX: name = "Linux"; break;
399 case IH_OS_VXWORKS: name = "VxWorks"; break;
400 case IH_OS_QNX: name = "QNX"; break;
401 case IH_OS_U_BOOT: name = "U-Boot"; break;
402 case IH_OS_RTEMS: name = "RTEMS"; break;
403#ifdef CONFIG_ARTOS
404 case IH_OS_ARTOS: name = "ARTOS"; break;
405#endif
406#ifdef CONFIG_LYNXKDI
407 case IH_OS_LYNXOS: name = "LynxOS"; break;
408#endif
409 default: name = "Unknown OS"; break;
410 }
411
412 return name;
413}
414
9a4daad0 415const char* genimg_get_arch_name (uint8_t arch)
42b73e8e
MB
416{
417 const char *name;
418
419 switch (arch) {
420 case IH_ARCH_INVALID: name = "Invalid Architecture"; break;
421 case IH_ARCH_ALPHA: name = "Alpha"; break;
422 case IH_ARCH_ARM: name = "ARM"; break;
423 case IH_ARCH_AVR32: name = "AVR32"; break;
424 case IH_ARCH_BLACKFIN: name = "Blackfin"; break;
425 case IH_ARCH_I386: name = "Intel x86"; break;
426 case IH_ARCH_IA64: name = "IA64"; break;
427 case IH_ARCH_M68K: name = "M68K"; break;
428 case IH_ARCH_MICROBLAZE:name = "Microblaze"; break;
429 case IH_ARCH_MIPS64: name = "MIPS 64 Bit"; break;
430 case IH_ARCH_MIPS: name = "MIPS"; break;
431 case IH_ARCH_NIOS2: name = "Nios-II"; break;
432 case IH_ARCH_NIOS: name = "Nios"; break;
433 case IH_ARCH_PPC: name = "PowerPC"; break;
434 case IH_ARCH_S390: name = "IBM S390"; break;
435 case IH_ARCH_SH: name = "SuperH"; break;
436 case IH_ARCH_SPARC64: name = "SPARC 64 Bit"; break;
437 case IH_ARCH_SPARC: name = "SPARC"; break;
438 default: name = "Unknown Architecture"; break;
439 }
440
441 return name;
442}
443
9a4daad0 444const char* genimg_get_type_name (uint8_t type)
42b73e8e
MB
445{
446 const char *name;
447
448 switch (type) {
449 case IH_TYPE_INVALID: name = "Invalid Image"; break;
450 case IH_TYPE_STANDALONE:name = "Standalone Program"; break;
451 case IH_TYPE_KERNEL: name = "Kernel Image"; break;
452 case IH_TYPE_RAMDISK: name = "RAMDisk Image"; break;
453 case IH_TYPE_MULTI: name = "Multi-File Image"; break;
454 case IH_TYPE_FIRMWARE: name = "Firmware"; break;
455 case IH_TYPE_SCRIPT: name = "Script"; break;
456 case IH_TYPE_FLATDT: name = "Flat Device Tree"; break;
457 default: name = "Unknown Image"; break;
458 }
459
460 return name;
461}
462
9a4daad0 463const char* genimg_get_comp_name (uint8_t comp)
42b73e8e
MB
464{
465 const char *name;
466
467 switch (comp) {
468 case IH_COMP_NONE: name = "uncompressed"; break;
469 case IH_COMP_GZIP: name = "gzip compressed"; break;
470 case IH_COMP_BZIP2: name = "bzip2 compressed"; break;
471 default: name = "unknown compression"; break;
472 }
473
474 return name;
475}
5ad03eb3 476
fff888a1 477/**
9a4daad0 478 * genimg_get_format - get image format type
fff888a1
MB
479 * @img_addr: image start address
480 *
9a4daad0 481 * genimg_get_format() checks whether provided address points to a valid
fff888a1
MB
482 * legacy or FIT image.
483 *
4efbe9db
MB
484 * New uImage format and FDT blob are based on a libfdt. FDT blob
485 * may be passed directly or embedded in a FIT image. In both situations
9a4daad0 486 * genimg_get_format() must be able to dectect libfdt header.
4efbe9db 487 *
fff888a1
MB
488 * returns:
489 * image format type or IMAGE_FORMAT_INVALID if no image is present
490 */
9a4daad0 491int genimg_get_format (void *img_addr)
fff888a1
MB
492{
493 ulong format = IMAGE_FORMAT_INVALID;
494 image_header_t *hdr;
4efbe9db 495#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
fff888a1
MB
496 char *fit_hdr;
497#endif
498
499 hdr = (image_header_t *)img_addr;
500 if (image_check_magic(hdr))
501 format = IMAGE_FORMAT_LEGACY;
4efbe9db 502#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
fff888a1
MB
503 else {
504 fit_hdr = (char *)img_addr;
505 if (fdt_check_header (fit_hdr) == 0)
506 format = IMAGE_FORMAT_FIT;
507 }
508#endif
509
510 return format;
511}
512
513/**
9a4daad0 514 * genimg_get_image - get image from special storage (if necessary)
fff888a1
MB
515 * @img_addr: image start address
516 *
9a4daad0 517 * genimg_get_image() checks if provided image start adddress is located
fff888a1
MB
518 * in a dataflash storage. If so, image is moved to a system RAM memory.
519 *
520 * returns:
521 * image start address after possible relocation from special storage
522 */
9a4daad0 523ulong genimg_get_image (ulong img_addr)
fff888a1 524{
6f0f9dfc 525 ulong ram_addr = img_addr;
fff888a1
MB
526
527#ifdef CONFIG_HAS_DATAFLASH
6f0f9dfc
MB
528 ulong h_size, d_size;
529
fff888a1 530 if (addr_dataflash (img_addr)){
6f0f9dfc 531 /* ger RAM address */
fff888a1 532 ram_addr = CFG_LOAD_ADDR;
6f0f9dfc
MB
533
534 /* get header size */
535 h_size = image_get_header_size ();
536#if defined(CONFIG_FIT)
537 if (sizeof(struct fdt_header) > h_size)
538 h_size = sizeof(struct fdt_header);
539#endif
540
541 /* read in header */
fff888a1
MB
542 debug (" Reading image header from dataflash address "
543 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
fff888a1 544
6f0f9dfc 545 read_dataflash (img_addr, h_size, (char *)ram_addr);
fff888a1 546
6f0f9dfc 547 /* get data size */
9a4daad0 548 switch (genimg_get_format ((void *)ram_addr)) {
6f0f9dfc
MB
549 case IMAGE_FORMAT_LEGACY:
550 d_size = image_get_data_size ((image_header_t *)ram_addr);
551 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
552 ram_addr, d_size);
553 break;
fff888a1 554#if defined(CONFIG_FIT)
6f0f9dfc
MB
555 case IMAGE_FORMAT_FIT:
556 d_size = fdt_totalsize((void *)ram_addr) - h_size;
557 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
558 ram_addr, d_size);
559 break;
fff888a1 560#endif
6f0f9dfc
MB
561 default:
562 printf (" No valid image found at 0x%08lx\n", img_addr);
563 return ram_addr;
564 }
fff888a1 565
6f0f9dfc 566 /* read in image data */
fff888a1
MB
567 debug (" Reading image remaining data from dataflash address "
568 "%08lx to RAM address %08lx\n", img_addr + h_size,
569 ram_addr + h_size);
570
571 read_dataflash (img_addr + h_size, d_size,
572 (char *)(ram_addr + h_size));
6f0f9dfc 573
fff888a1 574 }
6f0f9dfc 575#endif /* CONFIG_HAS_DATAFLASH */
fff888a1
MB
576
577 return ram_addr;
578}
579
5ad03eb3 580/**
9a4daad0 581 * boot_get_ramdisk - main ramdisk handling routine
5ad03eb3
MB
582 * @cmdtp: command table pointer
583 * @flag: command flag
584 * @argc: command argument count
585 * @argv: command argument list
8a5ea3e6 586 * @images: pointer to the bootm images structure
5ad03eb3
MB
587 * @arch: expected ramdisk architecture
588 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
589 * @rd_end: pointer to a ulong variable, will hold ramdisk end
590 *
9a4daad0 591 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
5ad03eb3
MB
592 * Curently supported are the following ramdisk sources:
593 * - multicomponent kernel/ramdisk image,
594 * - commandline provided address of decicated ramdisk image.
595 *
596 * returns:
597 * rd_start and rd_end are set to ramdisk start/end addresses if
598 * ramdisk image is found and valid
599 * rd_start and rd_end are set to 0 if no ramdisk exists
274cea2b 600 * return 1 if ramdisk image is found but corrupted
5ad03eb3 601 */
9a4daad0 602int boot_get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
8a5ea3e6 603 bootm_headers_t *images, uint8_t arch,
5ad03eb3
MB
604 ulong *rd_start, ulong *rd_end)
605{
d5934ad7 606 ulong rd_addr, rd_load;
5ad03eb3
MB
607 ulong rd_data, rd_len;
608 image_header_t *rd_hdr;
d5934ad7
MB
609#if defined(CONFIG_FIT)
610 void *fit_hdr;
611 const char *fit_uname_config = NULL;
612 const char *fit_uname_ramdisk = NULL;
613 ulong default_addr;
614#endif
5ad03eb3 615
d5934ad7
MB
616 /*
617 * Look for a '-' which indicates to ignore the
618 * ramdisk argument
619 */
620 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
621 debug ("## Skipping init Ramdisk\n");
622 rd_len = rd_data = 0;
623 } else if (argc >= 3) {
624#if defined(CONFIG_FIT)
5ad03eb3 625 /*
d5934ad7
MB
626 * If the init ramdisk comes from the FIT image and the FIT image
627 * address is omitted in the command line argument, try to use
628 * os FIT image address or default load address.
5ad03eb3 629 */
d5934ad7
MB
630 if (images->fit_uname_os)
631 default_addr = (ulong)images->fit_hdr_os;
632 else
633 default_addr = load_addr;
634
635 if (fit_parse_conf (argv[2], default_addr,
636 &rd_addr, &fit_uname_config)) {
637 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
638 fit_uname_config, rd_addr);
639 } else if (fit_parse_subimage (argv[2], default_addr,
640 &rd_addr, &fit_uname_ramdisk)) {
641 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
642 fit_uname_ramdisk, rd_addr);
643 } else
644#endif
645 {
646 rd_addr = simple_strtoul(argv[2], NULL, 16);
647 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
5ad03eb3 648 rd_addr);
d5934ad7
MB
649 }
650
651 /* copy from dataflash if needed */
652 printf ("## Loading init Ramdisk Image at %08lx ...\n",
653 rd_addr);
9a4daad0 654 rd_addr = genimg_get_image (rd_addr);
d5934ad7
MB
655
656 /*
657 * Check if there is an initrd image at the
658 * address provided in the second bootm argument
659 * check image type, for FIT images get FIT node.
660 */
9a4daad0 661 switch (genimg_get_format ((void *)rd_addr)) {
d5934ad7
MB
662 case IMAGE_FORMAT_LEGACY:
663
664 debug ("* ramdisk: legacy format image\n");
5ad03eb3
MB
665
666 rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
8a5ea3e6 667 rd_addr, arch, images->verify);
5ad03eb3 668
274cea2b
KG
669 if (rd_hdr == NULL) {
670 *rd_start = 0;
671 *rd_end = 0;
672 return 1;
673 }
674
5ad03eb3
MB
675 rd_data = image_get_data (rd_hdr);
676 rd_len = image_get_data_size (rd_hdr);
d5934ad7
MB
677 rd_load = image_get_load (rd_hdr);
678 break;
679#if defined(CONFIG_FIT)
680 case IMAGE_FORMAT_FIT:
681 fit_hdr = (void *)rd_addr;
682 debug ("* ramdisk: FIT format image\n");
683 fit_unsupported_reset ("ramdisk");
274cea2b 684 return 1;
d5934ad7
MB
685#endif
686 default:
687 printf ("Wrong Image Format for %s command\n",
688 cmdtp->name);
689 rd_data = rd_len = 0;
690 }
5ad03eb3
MB
691
692#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
d5934ad7
MB
693 /*
694 * We need to copy the ramdisk to SRAM to let Linux boot
695 */
696 if (rd_data) {
697 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
698 rd_data = rd_load;
5ad03eb3 699 }
d5934ad7 700#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
5ad03eb3 701
d5934ad7
MB
702 } else if (images->legacy_hdr_valid &&
703 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
5ad03eb3 704 /*
d5934ad7
MB
705 * Now check if we have a legacy mult-component image,
706 * get second entry data start address and len.
5ad03eb3
MB
707 */
708 show_boot_progress (13);
709 printf ("## Loading init Ramdisk from multi component "
d5934ad7
MB
710 "Image at %08lx ...\n",
711 (ulong)images->legacy_hdr_os);
712
713 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
5ad03eb3
MB
714 } else {
715 /*
716 * no initrd image
717 */
718 show_boot_progress (14);
719 rd_len = rd_data = 0;
720 }
721
722 if (!rd_data) {
723 debug ("## No init Ramdisk\n");
724 *rd_start = 0;
725 *rd_end = 0;
726 } else {
727 *rd_start = rd_data;
728 *rd_end = rd_data + rd_len;
729 }
730 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
731 *rd_start, *rd_end);
274cea2b
KG
732
733 return 0;
5ad03eb3 734}
ceaed2b1
MB
735
736#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
737/**
9a4daad0 738 * boot_ramdisk_high - relocate init ramdisk
e822d7fc 739 * @lmb: pointer to lmb handle, will be used for memory mgmt
ceaed2b1
MB
740 * @rd_data: ramdisk data start address
741 * @rd_len: ramdisk data length
ceaed2b1
MB
742 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
743 * start address (after possible relocation)
744 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
745 * end address (after possible relocation)
746 *
9a4daad0 747 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
ceaed2b1
MB
748 * variable and if requested ramdisk data is moved to a specified location.
749 *
9a4daad0
MB
750 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
751 * start/end addresses if ramdisk image start and len were provided,
752 * otherwise set initrd_start and initrd_end set to zeros.
753 *
ceaed2b1 754 * returns:
9a4daad0
MB
755 * 0 - success
756 * -1 - failure
ceaed2b1 757 */
9a4daad0 758int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
e822d7fc 759 ulong *initrd_start, ulong *initrd_end)
ceaed2b1
MB
760{
761 char *s;
762 ulong initrd_high;
763 int initrd_copy_to_ram = 1;
764
765 if ((s = getenv ("initrd_high")) != NULL) {
766 /* a value of "no" or a similar string will act like 0,
767 * turning the "load high" feature off. This is intentional.
768 */
769 initrd_high = simple_strtoul (s, NULL, 16);
770 if (initrd_high == ~0)
771 initrd_copy_to_ram = 0;
772 } else {
773 /* not set, no restrictions to load high */
774 initrd_high = ~0;
775 }
776
ceaed2b1
MB
777 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
778 initrd_high, initrd_copy_to_ram);
779
780 if (rd_data) {
781 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
782 debug (" in-place initrd\n");
783 *initrd_start = rd_data;
784 *initrd_end = rd_data + rd_len;
e822d7fc 785 lmb_reserve(lmb, rd_data, rd_len);
ceaed2b1 786 } else {
e822d7fc 787 if (initrd_high)
9a4daad0 788 *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
e822d7fc 789 else
9a4daad0 790 *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
e822d7fc
KG
791
792 if (*initrd_start == 0) {
9a4daad0 793 puts ("ramdisk - allocation error\n");
e822d7fc 794 goto error;
ceaed2b1 795 }
ceaed2b1
MB
796 show_boot_progress (12);
797
798 *initrd_end = *initrd_start + rd_len;
799 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
800 *initrd_start, *initrd_end);
801
9a4daad0 802 memmove_wd ((void *)*initrd_start,
ceaed2b1
MB
803 (void *)rd_data, rd_len, CHUNKSZ);
804
805 puts ("OK\n");
806 }
807 } else {
808 *initrd_start = 0;
809 *initrd_end = 0;
810 }
811 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
812 *initrd_start, *initrd_end);
9a4daad0 813
e822d7fc 814 return 0;
b6b0fe64 815
e822d7fc
KG
816error:
817 return -1;
b6b0fe64
MB
818}
819
820/**
9a4daad0 821 * boot_get_cmdline - allocate and initialize kernel cmdline
e822d7fc 822 * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe64
MB
823 * @cmd_start: pointer to a ulong variable, will hold cmdline start
824 * @cmd_end: pointer to a ulong variable, will hold cmdline end
e822d7fc
KG
825 * @bootmap_base: ulong variable, holds offset in physical memory to
826 * base of bootmap
b6b0fe64 827 *
9a4daad0 828 * boot_get_cmdline() allocates space for kernel command line below
e822d7fc 829 * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
b6b0fe64
MB
830 * variable is present its contents is copied to allocated kernel
831 * command line.
832 *
833 * returns:
e822d7fc
KG
834 * 0 - success
835 * -1 - failure
b6b0fe64 836 */
9a4daad0 837int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
e822d7fc 838 ulong bootmap_base)
b6b0fe64
MB
839{
840 char *cmdline;
841 char *s;
842
e822d7fc
KG
843 cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
844 CFG_BOOTMAPSZ + bootmap_base);
845
846 if (cmdline == NULL)
847 return -1;
b6b0fe64
MB
848
849 if ((s = getenv("bootargs")) == NULL)
850 s = "";
851
852 strcpy(cmdline, s);
853
854 *cmd_start = (ulong) & cmdline[0];
855 *cmd_end = *cmd_start + strlen(cmdline);
856
857 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
858
e822d7fc 859 return 0;
b6b0fe64
MB
860}
861
862/**
9a4daad0 863 * boot_get_kbd - allocate and initialize kernel copy of board info
e822d7fc 864 * @lmb: pointer to lmb handle, will be used for memory mgmt
b6b0fe64 865 * @kbd: double pointer to board info data
e822d7fc
KG
866 * @bootmap_base: ulong variable, holds offset in physical memory to
867 * base of bootmap
b6b0fe64 868 *
9a4daad0 869 * boot_get_kbd() allocates space for kernel copy of board info data below
e822d7fc
KG
870 * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
871 * the current u-boot board info data.
b6b0fe64
MB
872 *
873 * returns:
e822d7fc
KG
874 * 0 - success
875 * -1 - failure
b6b0fe64 876 */
9a4daad0 877int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
b6b0fe64 878{
e822d7fc
KG
879 *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
880 CFG_BOOTMAPSZ + bootmap_base);
881 if (*kbd == NULL)
882 return -1;
883
b6b0fe64
MB
884 **kbd = *(gd->bd);
885
886 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
887
888#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
889 do_bdinfo(NULL, 0, 0, NULL);
890#endif
891
e822d7fc 892 return 0;
ceaed2b1
MB
893}
894#endif /* CONFIG_PPC || CONFIG_M68K */
5ad03eb3 895
f50433d6
MB
896#if defined(CONFIG_FIT)
897/*****************************************************************************/
898/* New uImage format routines */
899/*****************************************************************************/
900static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
901 ulong *addr, const char **name)
902{
903 const char *sep;
904
905 *addr = addr_curr;
906 *name = NULL;
907
908 sep = strchr (spec, sepc);
909 if (sep) {
910 if (sep - spec > 0)
911 *addr = simple_strtoul (spec, NULL, 16);
912
913 *name = sep + 1;
914 return 1;
915 }
916
917 return 0;
918}
919
920/**
921 * fit_parse_conf - parse FIT configuration spec
922 * @spec: input string, containing configuration spec
923 * @add_curr: current image address (to be used as a possible default)
924 * @addr: pointer to a ulong variable, will hold FIT image address of a given
925 * configuration
926 * @conf_name double pointer to a char, will hold pointer to a configuration
927 * unit name
928 *
929 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
930 * where <addr> is a FIT image address that contains configuration
931 * with a <conf> unit name.
932 *
933 * Address part is optional, and if omitted default add_curr will
934 * be used instead.
935 *
936 * returns:
937 * 1 if spec is a valid configuration string,
938 * addr and conf_name are set accordingly
939 * 0 otherwise
940 */
941inline int fit_parse_conf (const char *spec, ulong addr_curr,
942 ulong *addr, const char **conf_name)
943{
944 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
945}
946
947/**
948 * fit_parse_subimage - parse FIT subimage spec
949 * @spec: input string, containing subimage spec
950 * @add_curr: current image address (to be used as a possible default)
951 * @addr: pointer to a ulong variable, will hold FIT image address of a given
952 * subimage
953 * @image_name: double pointer to a char, will hold pointer to a subimage name
954 *
955 * fit_parse_subimage() expects subimage spec in the for of
956 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
957 * subimage with a <subimg> unit name.
958 *
959 * Address part is optional, and if omitted default add_curr will
960 * be used instead.
961 *
962 * returns:
963 * 1 if spec is a valid subimage string,
964 * addr and image_name are set accordingly
965 * 0 otherwise
966 */
967inline int fit_parse_subimage (const char *spec, ulong addr_curr,
968 ulong *addr, const char **image_name)
969{
970 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
971}
d5934ad7 972
f50433d6
MB
973#endif /* CONFIG_FIT */
974
b6b0fe64 975#endif /* USE_HOSTCC */