blob: 92c067f0adeea80f85f5ad9b9e2ad8458ec5b6ce [file] [log] [blame]
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001/*
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 Balakowiczceaed2b2008-01-31 13:57:17 +010025
26#define DEBUG
27
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010028#ifndef USE_HOSTCC
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010029#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 Balakowiczceaed2b2008-01-31 13:57:17 +010040#ifdef CONFIG_LOGBUFFER
41#include <logbuff.h>
42#endif
43
Marian Balakowicz2242f532008-02-21 17:27:41 +010044#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
45#include <rtc.h>
46#endif
47
Marian Balakowiczfff888a12008-02-21 17:20:19 +010048#if defined(CONFIG_FIT)
49#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
52#endif
53
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010054extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010055
56#ifdef CONFIG_CMD_BDI
57extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
58#endif
59
60DECLARE_GLOBAL_DATA_PTR;
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010061
62static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
63 int argc, char *argv[],
64 ulong rd_addr, uint8_t arch, int verify);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010065#else
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +010066#include "mkimage.h"
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +010067#endif /* USE_HOSTCC*/
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010068
69#include <image.h>
70
71unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
72
73int image_check_hcrc (image_header_t *hdr)
74{
75 ulong hcrc;
76 ulong len = image_get_header_size ();
77 image_header_t header;
78
79 /* Copy header so we can blank CRC field for re-calculation */
80 memmove (&header, (char *)hdr, image_get_header_size ());
81 image_set_hcrc (&header, 0);
82
83 hcrc = crc32 (0, (unsigned char *)&header, len);
84
85 return (hcrc == image_get_hcrc (hdr));
86}
87
88int image_check_dcrc (image_header_t *hdr)
89{
90 ulong data = image_get_data (hdr);
91 ulong len = image_get_data_size (hdr);
92 ulong dcrc = crc32 (0, (unsigned char *)data, len);
93
94 return (dcrc == image_get_dcrc (hdr));
95}
96
Marian Balakowiczaf13cdb2008-01-08 18:11:45 +010097#ifndef USE_HOSTCC
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010098int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
99{
100 ulong dcrc = 0;
101 ulong len = image_get_data_size (hdr);
102 ulong data = image_get_data (hdr);
103
104#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
105 ulong cdata = data;
106 ulong edata = cdata + len;
107
108 while (cdata < edata) {
109 ulong chunk = edata - cdata;
110
111 if (chunk > chunksz)
112 chunk = chunksz;
113 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
114 cdata += chunk;
115
116 WATCHDOG_RESET ();
117 }
118#else
119 dcrc = crc32 (0, (unsigned char *)data, len);
120#endif
121
122 return (dcrc == image_get_dcrc (hdr));
123}
124
125int getenv_verify (void)
126{
127 char *s = getenv ("verify");
128 return (s && (*s == 'n')) ? 0 : 1;
129}
Marian Balakowiczaf13cdb2008-01-08 18:11:45 +0100130
131void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
132{
133#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
134 while (len > 0) {
135 size_t tail = (len > chunksz) ? chunksz : len;
136 WATCHDOG_RESET ();
137 memmove (to, from, tail);
138 to += tail;
139 from += tail;
140 len -= tail;
141 }
142#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
143 memmove (to, from, len);
144#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
145}
146#endif /* USE_HOSTCC */
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100147
148/**
149 * image_multi_count - get component (sub-image) count
150 * @hdr: pointer to the header of the multi component image
151 *
152 * image_multi_count() returns number of components in a multi
153 * component image.
154 *
155 * Note: no checking of the image type is done, caller must pass
156 * a valid multi component image.
157 *
158 * returns:
159 * number of components
160 */
161ulong image_multi_count (image_header_t *hdr)
162{
163 ulong i, count = 0;
164 ulong *size;
165
166 /* get start of the image payload, which in case of multi
167 * component images that points to a table of component sizes */
168 size = (ulong *)image_get_data (hdr);
169
170 /* count non empty slots */
171 for (i = 0; size[i]; ++i)
172 count++;
173
174 return count;
175}
176
177/**
178 * image_multi_getimg - get component data address and size
179 * @hdr: pointer to the header of the multi component image
180 * @idx: index of the requested component
181 * @data: pointer to a ulong variable, will hold component data address
182 * @len: pointer to a ulong variable, will hold component size
183 *
184 * image_multi_getimg() returns size and data address for the requested
185 * component in a multi component image.
186 *
187 * Note: no checking of the image type is done, caller must pass
188 * a valid multi component image.
189 *
190 * returns:
191 * data address and size of the component, if idx is valid
192 * 0 in data and len, if idx is out of range
193 */
194void image_multi_getimg (image_header_t *hdr, ulong idx,
195 ulong *data, ulong *len)
196{
197 int i;
198 ulong *size;
199 ulong offset, tail, count, img_data;
200
201 /* get number of component */
202 count = image_multi_count (hdr);
203
204 /* get start of the image payload, which in case of multi
205 * component images that points to a table of component sizes */
206 size = (ulong *)image_get_data (hdr);
207
208 /* get address of the proper component data start, which means
209 * skipping sizes table (add 1 for last, null entry) */
210 img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong);
211
212 if (idx < count) {
213 *len = size[idx];
214 offset = 0;
215 tail = 0;
216
217 /* go over all indices preceding requested component idx */
218 for (i = 0; i < idx; i++) {
219 /* add up i-th component size */
220 offset += size[i];
221
222 /* add up alignment for i-th component */
223 tail += (4 - size[i] % 4);
224 }
225
226 /* calculate idx-th component data address */
227 *data = img_data + offset + tail;
228 } else {
229 *len = 0;
230 *data = 0;
231 }
232}
Marian Balakowicz42b73e82008-01-31 13:20:07 +0100233
234#ifndef USE_HOSTCC
235const char* image_get_os_name (uint8_t os)
236{
237 const char *name;
238
239 switch (os) {
240 case IH_OS_INVALID: name = "Invalid OS"; break;
241 case IH_OS_NETBSD: name = "NetBSD"; break;
242 case IH_OS_LINUX: name = "Linux"; break;
243 case IH_OS_VXWORKS: name = "VxWorks"; break;
244 case IH_OS_QNX: name = "QNX"; break;
245 case IH_OS_U_BOOT: name = "U-Boot"; break;
246 case IH_OS_RTEMS: name = "RTEMS"; break;
247#ifdef CONFIG_ARTOS
248 case IH_OS_ARTOS: name = "ARTOS"; break;
249#endif
250#ifdef CONFIG_LYNXKDI
251 case IH_OS_LYNXOS: name = "LynxOS"; break;
252#endif
253 default: name = "Unknown OS"; break;
254 }
255
256 return name;
257}
258
259const char* image_get_arch_name (uint8_t arch)
260{
261 const char *name;
262
263 switch (arch) {
264 case IH_ARCH_INVALID: name = "Invalid Architecture"; break;
265 case IH_ARCH_ALPHA: name = "Alpha"; break;
266 case IH_ARCH_ARM: name = "ARM"; break;
267 case IH_ARCH_AVR32: name = "AVR32"; break;
268 case IH_ARCH_BLACKFIN: name = "Blackfin"; break;
269 case IH_ARCH_I386: name = "Intel x86"; break;
270 case IH_ARCH_IA64: name = "IA64"; break;
271 case IH_ARCH_M68K: name = "M68K"; break;
272 case IH_ARCH_MICROBLAZE:name = "Microblaze"; break;
273 case IH_ARCH_MIPS64: name = "MIPS 64 Bit"; break;
274 case IH_ARCH_MIPS: name = "MIPS"; break;
275 case IH_ARCH_NIOS2: name = "Nios-II"; break;
276 case IH_ARCH_NIOS: name = "Nios"; break;
277 case IH_ARCH_PPC: name = "PowerPC"; break;
278 case IH_ARCH_S390: name = "IBM S390"; break;
279 case IH_ARCH_SH: name = "SuperH"; break;
280 case IH_ARCH_SPARC64: name = "SPARC 64 Bit"; break;
281 case IH_ARCH_SPARC: name = "SPARC"; break;
282 default: name = "Unknown Architecture"; break;
283 }
284
285 return name;
286}
287
288const char* image_get_type_name (uint8_t type)
289{
290 const char *name;
291
292 switch (type) {
293 case IH_TYPE_INVALID: name = "Invalid Image"; break;
294 case IH_TYPE_STANDALONE:name = "Standalone Program"; break;
295 case IH_TYPE_KERNEL: name = "Kernel Image"; break;
296 case IH_TYPE_RAMDISK: name = "RAMDisk Image"; break;
297 case IH_TYPE_MULTI: name = "Multi-File Image"; break;
298 case IH_TYPE_FIRMWARE: name = "Firmware"; break;
299 case IH_TYPE_SCRIPT: name = "Script"; break;
300 case IH_TYPE_FLATDT: name = "Flat Device Tree"; break;
301 default: name = "Unknown Image"; break;
302 }
303
304 return name;
305}
306
307const char* image_get_comp_name (uint8_t comp)
308{
309 const char *name;
310
311 switch (comp) {
312 case IH_COMP_NONE: name = "uncompressed"; break;
313 case IH_COMP_GZIP: name = "gzip compressed"; break;
314 case IH_COMP_BZIP2: name = "bzip2 compressed"; break;
315 default: name = "unknown compression"; break;
316 }
317
318 return name;
319}
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100320
Marian Balakowicz2242f532008-02-21 17:27:41 +0100321static void image_print_type (image_header_t *hdr)
322{
323 const char *os, *arch, *type, *comp;
324
325 os = image_get_os_name (image_get_os (hdr));
326 arch = image_get_arch_name (image_get_arch (hdr));
327 type = image_get_type_name (image_get_type (hdr));
328 comp = image_get_comp_name (image_get_comp (hdr));
329
330 printf ("%s %s %s (%s)", arch, os, type, comp);
331}
332
333void image_print_contents (image_header_t *hdr)
334{
335#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
336 time_t timestamp = (time_t)image_get_time (hdr);
337 struct rtc_time tm;
338#endif
339
340 printf (" Image Name: %.*s\n", IH_NMLEN, image_get_name (hdr));
341
342#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
343 to_tm (timestamp, &tm);
344 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
345 tm.tm_year, tm.tm_mon, tm.tm_mday,
346 tm.tm_hour, tm.tm_min, tm.tm_sec);
347#endif
348 puts (" Image Type: ");
349 image_print_type (hdr);
350
351 printf ("\n Data Size: %d Bytes = ", image_get_data_size (hdr));
352 print_size (image_get_data_size (hdr), "\n");
353 printf (" Load Address: %08x\n"
354 " Entry Point: %08x\n",
355 image_get_load (hdr), image_get_ep (hdr));
356
357 if (image_check_type (hdr, IH_TYPE_MULTI)) {
358 int i;
359 ulong data, len;
360 ulong count = image_multi_count (hdr);
361
362 puts (" Contents:\n");
363 for (i = 0; i < count; i++) {
364 image_multi_getimg (hdr, i, &data, &len);
365 printf (" Image %d: %8ld Bytes = ", i, len);
366 print_size (len, "\n");
367 }
368 }
369}
370
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100371/**
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100372 * gen_image_get_format - get image format type
373 * @img_addr: image start address
374 *
375 * gen_image_get_format() checks whether provided address points to a valid
376 * legacy or FIT image.
377 *
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100378 * New uImage format and FDT blob are based on a libfdt. FDT blob
379 * may be passed directly or embedded in a FIT image. In both situations
380 * gen_image_get_format() must be able to dectect libfdt header.
381 *
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100382 * returns:
383 * image format type or IMAGE_FORMAT_INVALID if no image is present
384 */
385int gen_image_get_format (void *img_addr)
386{
387 ulong format = IMAGE_FORMAT_INVALID;
388 image_header_t *hdr;
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100389#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100390 char *fit_hdr;
391#endif
392
393 hdr = (image_header_t *)img_addr;
394 if (image_check_magic(hdr))
395 format = IMAGE_FORMAT_LEGACY;
Marian Balakowicz4efbe9d2008-02-27 11:02:26 +0100396#if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100397 else {
398 fit_hdr = (char *)img_addr;
399 if (fdt_check_header (fit_hdr) == 0)
400 format = IMAGE_FORMAT_FIT;
401 }
402#endif
403
404 return format;
405}
406
407/**
408 * gen_get_image - get image from special storage (if necessary)
409 * @img_addr: image start address
410 *
411 * gen_get_image() checks if provided image start adddress is located
412 * in a dataflash storage. If so, image is moved to a system RAM memory.
413 *
414 * returns:
415 * image start address after possible relocation from special storage
416 */
417ulong gen_get_image (ulong img_addr)
418{
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100419 ulong ram_addr = img_addr;
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100420
421#ifdef CONFIG_HAS_DATAFLASH
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100422 ulong h_size, d_size;
423
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100424 if (addr_dataflash (img_addr)){
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100425 /* ger RAM address */
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100426 ram_addr = CFG_LOAD_ADDR;
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100427
428 /* get header size */
429 h_size = image_get_header_size ();
430#if defined(CONFIG_FIT)
431 if (sizeof(struct fdt_header) > h_size)
432 h_size = sizeof(struct fdt_header);
433#endif
434
435 /* read in header */
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100436 debug (" Reading image header from dataflash address "
437 "%08lx to RAM address %08lx\n", img_addr, ram_addr);
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100438
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100439 read_dataflash (img_addr, h_size, (char *)ram_addr);
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100440
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100441 /* get data size */
442 switch (gen_image_get_format ((void *)ram_addr)) {
443 case IMAGE_FORMAT_LEGACY:
444 d_size = image_get_data_size ((image_header_t *)ram_addr);
445 debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
446 ram_addr, d_size);
447 break;
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100448#if defined(CONFIG_FIT)
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100449 case IMAGE_FORMAT_FIT:
450 d_size = fdt_totalsize((void *)ram_addr) - h_size;
451 debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
452 ram_addr, d_size);
453 break;
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100454#endif
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100455 default:
456 printf (" No valid image found at 0x%08lx\n", img_addr);
457 return ram_addr;
458 }
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100459
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100460 /* read in image data */
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100461 debug (" Reading image remaining data from dataflash address "
462 "%08lx to RAM address %08lx\n", img_addr + h_size,
463 ram_addr + h_size);
464
465 read_dataflash (img_addr + h_size, d_size,
466 (char *)(ram_addr + h_size));
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100467
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100468 }
Marian Balakowicz6f0f9df2008-02-27 11:00:47 +0100469#endif /* CONFIG_HAS_DATAFLASH */
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100470
471 return ram_addr;
472}
473
474/**
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100475 * image_get_ramdisk - get and verify ramdisk image
476 * @cmdtp: command table pointer
477 * @flag: command flag
478 * @argc: command argument count
479 * @argv: command argument list
480 * @rd_addr: ramdisk image start address
481 * @arch: expected ramdisk architecture
482 * @verify: checksum verification flag
483 *
484 * image_get_ramdisk() returns a pointer to the verified ramdisk image
485 * header. Routine receives image start address and expected architecture
486 * flag. Verification done covers data and header integrity and os/type/arch
487 * fields checking.
488 *
489 * If dataflash support is enabled routine checks for dataflash addresses
490 * and handles required dataflash reads.
491 *
492 * returns:
493 * pointer to a ramdisk image header, if image was found and valid
494 * otherwise, board is reset
495 */
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100496static image_header_t* image_get_ramdisk (cmd_tbl_t *cmdtp, int flag,
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100497 int argc, char *argv[],
498 ulong rd_addr, uint8_t arch, int verify)
499{
500 image_header_t *rd_hdr;
501
502 show_boot_progress (9);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100503 rd_hdr = (image_header_t *)rd_addr;
504
505 if (!image_check_magic (rd_hdr)) {
506 puts ("Bad Magic Number\n");
507 show_boot_progress (-10);
508 do_reset (cmdtp, flag, argc, argv);
509 }
510
511 if (!image_check_hcrc (rd_hdr)) {
512 puts ("Bad Header Checksum\n");
513 show_boot_progress (-11);
514 do_reset (cmdtp, flag, argc, argv);
515 }
516
517 show_boot_progress (10);
Marian Balakowicz2242f532008-02-21 17:27:41 +0100518 image_print_contents (rd_hdr);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100519
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100520 if (verify) {
521 puts(" Verifying Checksum ... ");
522 if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
523 puts ("Bad Data CRC\n");
524 show_boot_progress (-12);
525 do_reset (cmdtp, flag, argc, argv);
526 }
527 puts("OK\n");
528 }
529
530 show_boot_progress (11);
531
532 if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
533 !image_check_arch (rd_hdr, arch) ||
534 !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
535 printf ("No Linux %s Ramdisk Image\n",
536 image_get_arch_name(arch));
537 show_boot_progress (-13);
538 do_reset (cmdtp, flag, argc, argv);
539 }
540
541 return rd_hdr;
542}
543
544/**
545 * get_ramdisk - main ramdisk handling routine
546 * @cmdtp: command table pointer
547 * @flag: command flag
548 * @argc: command argument count
549 * @argv: command argument list
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100550 * @images: pointer to the bootm images structure
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100551 * @arch: expected ramdisk architecture
552 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
553 * @rd_end: pointer to a ulong variable, will hold ramdisk end
554 *
555 * get_ramdisk() is responsible for finding a valid ramdisk image.
556 * Curently supported are the following ramdisk sources:
557 * - multicomponent kernel/ramdisk image,
558 * - commandline provided address of decicated ramdisk image.
559 *
560 * returns:
561 * rd_start and rd_end are set to ramdisk start/end addresses if
562 * ramdisk image is found and valid
563 * rd_start and rd_end are set to 0 if no ramdisk exists
564 * board is reset if ramdisk image is found but corrupted
565 */
566void get_ramdisk (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100567 bootm_headers_t *images, uint8_t arch,
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100568 ulong *rd_start, ulong *rd_end)
569{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100570 ulong rd_addr, rd_load;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100571 ulong rd_data, rd_len;
572 image_header_t *rd_hdr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100573#if defined(CONFIG_FIT)
574 void *fit_hdr;
575 const char *fit_uname_config = NULL;
576 const char *fit_uname_ramdisk = NULL;
577 ulong default_addr;
578#endif
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100579
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100580 /*
581 * Look for a '-' which indicates to ignore the
582 * ramdisk argument
583 */
584 if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
585 debug ("## Skipping init Ramdisk\n");
586 rd_len = rd_data = 0;
587 } else if (argc >= 3) {
588#if defined(CONFIG_FIT)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100589 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100590 * If the init ramdisk comes from the FIT image and the FIT image
591 * address is omitted in the command line argument, try to use
592 * os FIT image address or default load address.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100593 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100594 if (images->fit_uname_os)
595 default_addr = (ulong)images->fit_hdr_os;
596 else
597 default_addr = load_addr;
598
599 if (fit_parse_conf (argv[2], default_addr,
600 &rd_addr, &fit_uname_config)) {
601 debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
602 fit_uname_config, rd_addr);
603 } else if (fit_parse_subimage (argv[2], default_addr,
604 &rd_addr, &fit_uname_ramdisk)) {
605 debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
606 fit_uname_ramdisk, rd_addr);
607 } else
608#endif
609 {
610 rd_addr = simple_strtoul(argv[2], NULL, 16);
611 debug ("* ramdisk: cmdline image address = 0x%08lx\n",
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100612 rd_addr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100613 }
614
615 /* copy from dataflash if needed */
616 printf ("## Loading init Ramdisk Image at %08lx ...\n",
617 rd_addr);
618 rd_addr = gen_get_image (rd_addr);
619
620 /*
621 * Check if there is an initrd image at the
622 * address provided in the second bootm argument
623 * check image type, for FIT images get FIT node.
624 */
625 switch (gen_image_get_format ((void *)rd_addr)) {
626 case IMAGE_FORMAT_LEGACY:
627
628 debug ("* ramdisk: legacy format image\n");
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100629
630 rd_hdr = image_get_ramdisk (cmdtp, flag, argc, argv,
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100631 rd_addr, arch, images->verify);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100632
633 rd_data = image_get_data (rd_hdr);
634 rd_len = image_get_data_size (rd_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100635 rd_load = image_get_load (rd_hdr);
636 break;
637#if defined(CONFIG_FIT)
638 case IMAGE_FORMAT_FIT:
639 fit_hdr = (void *)rd_addr;
640 debug ("* ramdisk: FIT format image\n");
641 fit_unsupported_reset ("ramdisk");
642 do_reset (cmdtp, flag, argc, argv);
643#endif
644 default:
645 printf ("Wrong Image Format for %s command\n",
646 cmdtp->name);
647 rd_data = rd_len = 0;
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100648 }
649
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100650#if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100651 /*
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100652 * We need to copy the ramdisk to SRAM to let Linux boot
653 */
654 if (rd_data) {
655 memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
656 rd_data = rd_load;
657 }
658#endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
659
660 } else if (images->legacy_hdr_valid &&
661 image_check_type (images->legacy_hdr_os, IH_TYPE_MULTI)) {
662 /*
663 * Now check if we have a legacy mult-component image,
664 * get second entry data start address and len.
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100665 */
666 show_boot_progress (13);
667 printf ("## Loading init Ramdisk from multi component "
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100668 "Image at %08lx ...\n",
669 (ulong)images->legacy_hdr_os);
670
671 image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100672 } else {
673 /*
674 * no initrd image
675 */
676 show_boot_progress (14);
677 rd_len = rd_data = 0;
678 }
679
680 if (!rd_data) {
681 debug ("## No init Ramdisk\n");
682 *rd_start = 0;
683 *rd_end = 0;
684 } else {
685 *rd_start = rd_data;
686 *rd_end = rd_data + rd_len;
687 }
688 debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
689 *rd_start, *rd_end);
690}
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100691
692#if defined(CONFIG_PPC) || defined(CONFIG_M68K)
693/**
694 * ramdisk_high - relocate init ramdisk
695 * @rd_data: ramdisk data start address
696 * @rd_len: ramdisk data length
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100697 * @sp_limit: stack pointer limit (including BOOTMAPSZ)
698 * @sp: current stack pointer
699 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
700 * start address (after possible relocation)
701 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
702 * end address (after possible relocation)
703 *
704 * ramdisk_high() takes a relocation hint from "initrd_high" environement
705 * variable and if requested ramdisk data is moved to a specified location.
706 *
707 * returns:
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100708 * - initrd_start and initrd_end are set to final (after relocation) ramdisk
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100709 * start/end addresses if ramdisk image start and len were provided
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100710 * otherwise set initrd_start and initrd_end set to zeros
711 * - returns new allc_current, next free address below BOOTMAPSZ
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100712 */
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100713ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len,
Kumar Galaa6612bd2008-02-27 21:51:43 -0600714 ulong sp_limit, ulong sp,
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100715 ulong *initrd_start, ulong *initrd_end)
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100716{
717 char *s;
718 ulong initrd_high;
719 int initrd_copy_to_ram = 1;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100720 ulong new_alloc_current = alloc_current;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100721
722 if ((s = getenv ("initrd_high")) != NULL) {
723 /* a value of "no" or a similar string will act like 0,
724 * turning the "load high" feature off. This is intentional.
725 */
726 initrd_high = simple_strtoul (s, NULL, 16);
727 if (initrd_high == ~0)
728 initrd_copy_to_ram = 0;
729 } else {
730 /* not set, no restrictions to load high */
731 initrd_high = ~0;
732 }
733
734#ifdef CONFIG_LOGBUFFER
735 /* Prevent initrd from overwriting logbuffer */
Kumar Galaa6612bd2008-02-27 21:51:43 -0600736 if (initrd_high < (gd->bd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD))
737 initrd_high = gd->bd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD;
738 debug ("## Logbuffer at 0x%08lx ", gd->bd->bi_memsize - LOGBUFF_LEN);
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100739#endif
740 debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
741 initrd_high, initrd_copy_to_ram);
742
743 if (rd_data) {
744 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
745 debug (" in-place initrd\n");
746 *initrd_start = rd_data;
747 *initrd_end = rd_data + rd_len;
748 } else {
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100749 new_alloc_current = alloc_current - rd_len;
750 *initrd_start = new_alloc_current;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100751 *initrd_start &= ~(4096 - 1); /* align on page */
752
753 if (initrd_high) {
754 ulong nsp;
755
756 /*
757 * the inital ramdisk does not need to be within
758 * CFG_BOOTMAPSZ as it is not accessed until after
759 * the mm system is initialised.
760 *
761 * do the stack bottom calculation again and see if
762 * the initrd will fit just below the monitor stack
763 * bottom without overwriting the area allocated
764 * for command line args and board info.
765 */
766 nsp = sp;
767 nsp -= 2048; /* just to be sure */
768 nsp &= ~0xF;
769
770 if (nsp > initrd_high) /* limit as specified */
771 nsp = initrd_high;
772
773 nsp -= rd_len;
774 nsp &= ~(4096 - 1); /* align on page */
775
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100776 if (nsp >= sp_limit) {
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100777 *initrd_start = nsp;
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100778 new_alloc_current = alloc_current;
779 }
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100780 }
781
782 show_boot_progress (12);
783
784 *initrd_end = *initrd_start + rd_len;
785 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
786 *initrd_start, *initrd_end);
787
788 memmove_wd((void *)*initrd_start,
789 (void *)rd_data, rd_len, CHUNKSZ);
790
791 puts ("OK\n");
792 }
793 } else {
794 *initrd_start = 0;
795 *initrd_end = 0;
796 }
797 debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
798 *initrd_start, *initrd_end);
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100799
800 return new_alloc_current;
801}
802
803/**
804 * get_boot_sp_limit - calculate stack pointer limit
805 * @sp: current stack pointer
806 *
807 * get_boot_sp_limit() takes current stack pointer adrress and calculates
808 * stack pointer limit, below which kernel boot data (cmdline, board info,
809 * etc.) will be allocated.
810 *
811 * returns:
812 * stack pointer limit
813 */
814ulong get_boot_sp_limit(ulong sp)
815{
816 ulong sp_limit = sp;
817
818 sp_limit -= 2048; /* just to be sure */
819
820 /* make sure sp_limit is within kernel mapped space */
821 if (sp_limit > CFG_BOOTMAPSZ)
822 sp_limit = CFG_BOOTMAPSZ;
823 sp_limit &= ~0xF;
824
825 return sp_limit;
826}
827
828/**
829 * get_boot_cmdline - allocate and initialize kernel cmdline
830 * @alloc_current: current boot allocation address (counting down
831 * from sp_limit)
832 * @cmd_start: pointer to a ulong variable, will hold cmdline start
833 * @cmd_end: pointer to a ulong variable, will hold cmdline end
834 *
835 * get_boot_cmdline() allocates space for kernel command line below
836 * provided alloc_current address. If "bootargs" U-boot environemnt
837 * variable is present its contents is copied to allocated kernel
838 * command line.
839 *
840 * returns:
841 * alloc_current after cmdline allocation
842 */
843ulong get_boot_cmdline (ulong alloc_current, ulong *cmd_start, ulong *cmd_end)
844{
845 char *cmdline;
846 char *s;
847
848 cmdline = (char *)((alloc_current - CFG_BARGSIZE) & ~0xF);
849
850 if ((s = getenv("bootargs")) == NULL)
851 s = "";
852
853 strcpy(cmdline, s);
854
855 *cmd_start = (ulong) & cmdline[0];
856 *cmd_end = *cmd_start + strlen(cmdline);
857
858 debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
859
860 return (ulong)cmdline;
861}
862
863/**
864 * get_boot_kbd - allocate and initialize kernel copy of board info
865 * @alloc_current: current boot allocation address (counting down
866 * from sp_limit)
867 * @kbd: double pointer to board info data
868 *
869 * get_boot_kbd() - allocates space for kernel copy of board info data.
870 * Space is allocated below provided alloc_current address and kernel
871 * board info is initialized with the current u-boot board info data.
872 *
873 * returns:
874 * alloc_current after kbd allocation
875 */
876ulong get_boot_kbd (ulong alloc_current, bd_t **kbd)
877{
878 *kbd = (bd_t *) (((ulong)alloc_current - sizeof(bd_t)) & ~0xF);
879 **kbd = *(gd->bd);
880
881 debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
882
883#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
884 do_bdinfo(NULL, 0, 0, NULL);
885#endif
886
887 return (ulong)*kbd;
Marian Balakowiczceaed2b2008-01-31 13:57:17 +0100888}
889#endif /* CONFIG_PPC || CONFIG_M68K */
Marian Balakowicz5ad03eb2008-01-31 13:55:39 +0100890
Marian Balakowiczf50433d2008-02-21 17:20:20 +0100891#if defined(CONFIG_FIT)
892/*****************************************************************************/
893/* New uImage format routines */
894/*****************************************************************************/
895static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
896 ulong *addr, const char **name)
897{
898 const char *sep;
899
900 *addr = addr_curr;
901 *name = NULL;
902
903 sep = strchr (spec, sepc);
904 if (sep) {
905 if (sep - spec > 0)
906 *addr = simple_strtoul (spec, NULL, 16);
907
908 *name = sep + 1;
909 return 1;
910 }
911
912 return 0;
913}
914
915/**
916 * fit_parse_conf - parse FIT configuration spec
917 * @spec: input string, containing configuration spec
918 * @add_curr: current image address (to be used as a possible default)
919 * @addr: pointer to a ulong variable, will hold FIT image address of a given
920 * configuration
921 * @conf_name double pointer to a char, will hold pointer to a configuration
922 * unit name
923 *
924 * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
925 * where <addr> is a FIT image address that contains configuration
926 * with a <conf> unit name.
927 *
928 * Address part is optional, and if omitted default add_curr will
929 * be used instead.
930 *
931 * returns:
932 * 1 if spec is a valid configuration string,
933 * addr and conf_name are set accordingly
934 * 0 otherwise
935 */
936inline int fit_parse_conf (const char *spec, ulong addr_curr,
937 ulong *addr, const char **conf_name)
938{
939 return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
940}
941
942/**
943 * fit_parse_subimage - parse FIT subimage spec
944 * @spec: input string, containing subimage spec
945 * @add_curr: current image address (to be used as a possible default)
946 * @addr: pointer to a ulong variable, will hold FIT image address of a given
947 * subimage
948 * @image_name: double pointer to a char, will hold pointer to a subimage name
949 *
950 * fit_parse_subimage() expects subimage spec in the for of
951 * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
952 * subimage with a <subimg> unit name.
953 *
954 * Address part is optional, and if omitted default add_curr will
955 * be used instead.
956 *
957 * returns:
958 * 1 if spec is a valid subimage string,
959 * addr and image_name are set accordingly
960 * 0 otherwise
961 */
962inline int fit_parse_subimage (const char *spec, ulong addr_curr,
963 ulong *addr, const char **image_name)
964{
965 return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
966}
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100967
Marian Balakowiczf50433d2008-02-21 17:20:20 +0100968#endif /* CONFIG_FIT */
969
Marian Balakowiczb6b0fe62008-01-31 13:58:13 +0100970#endif /* USE_HOSTCC */