]>
Commit | Line | Data |
---|---|---|
47d1a6e1 WD |
1 | /* |
2 | * (C) Copyright 2000-2002 | |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | * | |
5 | * See file CREDITS for list of people who contributed to this | |
6 | * project. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License as | |
10 | * published by the Free Software Foundation; either version 2 of | |
11 | * the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | * MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | /* | |
25 | * Boot support | |
26 | */ | |
27 | #include <common.h> | |
28 | #include <watchdog.h> | |
29 | #include <command.h> | |
47d1a6e1 WD |
30 | #include <image.h> |
31 | #include <malloc.h> | |
32 | #include <zlib.h> | |
c29fdfc1 | 33 | #include <bzlib.h> |
7f70e853 | 34 | #include <environment.h> |
47d1a6e1 | 35 | #include <asm/byteorder.h> |
8bde7f77 WD |
36 | |
37 | /*cmd_boot.c*/ | |
38 | extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); | |
39 | ||
47d1a6e1 WD |
40 | #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP) |
41 | #include <rtc.h> | |
42 | #endif | |
43 | ||
44 | #ifdef CFG_HUSH_PARSER | |
45 | #include <hush.h> | |
46 | #endif | |
47 | ||
48 | #ifdef CONFIG_SHOW_BOOT_PROGRESS | |
49 | # include <status_led.h> | |
50 | # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg) | |
51 | #else | |
52 | # define SHOW_BOOT_PROGRESS(arg) | |
53 | #endif | |
54 | ||
55 | #ifdef CFG_INIT_RAM_LOCK | |
56 | #include <asm/cache.h> | |
57 | #endif | |
58 | ||
228f29ac WD |
59 | #ifdef CONFIG_LOGBUFFER |
60 | #include <logbuff.h> | |
61 | #endif | |
62 | ||
2abbe075 WD |
63 | #ifdef CONFIG_HAS_DATAFLASH |
64 | #include <dataflash.h> | |
65 | #endif | |
66 | ||
47d1a6e1 WD |
67 | /* |
68 | * Some systems (for example LWMON) have very short watchdog periods; | |
69 | * we must make sure to split long operations like memmove() or | |
70 | * crc32() into reasonable chunks. | |
71 | */ | |
72 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
73 | # define CHUNKSZ (64 * 1024) | |
74 | #endif | |
75 | ||
eedcd078 | 76 | int gunzip (void *, int, unsigned char *, unsigned long *); |
47d1a6e1 WD |
77 | |
78 | static void *zalloc(void *, unsigned, unsigned); | |
79 | static void zfree(void *, void *, unsigned); | |
80 | ||
81 | #if (CONFIG_COMMANDS & CFG_CMD_IMI) | |
82 | static int image_info (unsigned long addr); | |
83 | #endif | |
27b207fd WD |
84 | |
85 | #if (CONFIG_COMMANDS & CFG_CMD_IMLS) | |
86 | #include <flash.h> | |
87 | extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */ | |
88 | static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); | |
89 | #endif | |
90 | ||
47d1a6e1 WD |
91 | static void print_type (image_header_t *hdr); |
92 | ||
2262cfee WD |
93 | #ifdef __I386__ |
94 | image_header_t *fake_header(image_header_t *hdr, void *ptr, int size); | |
95 | #endif | |
96 | ||
47d1a6e1 WD |
97 | /* |
98 | * Continue booting an OS image; caller already has: | |
99 | * - copied image header to global variable `header' | |
100 | * - checked header magic number, checksums (both header & image), | |
101 | * - verified image architecture (PPC) and type (KERNEL or MULTI), | |
102 | * - loaded (first part of) image to header load address, | |
103 | * - disabled interrupts. | |
104 | */ | |
105 | typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag, | |
106 | int argc, char *argv[], | |
107 | ulong addr, /* of image to boot */ | |
108 | ulong *len_ptr, /* multi-file image length table */ | |
109 | int verify); /* getenv("verify")[0] != 'n' */ | |
110 | ||
8bde7f77 WD |
111 | #ifdef DEBUG |
112 | extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); | |
113 | #endif | |
114 | ||
2262cfee | 115 | #ifdef CONFIG_PPC |
47d1a6e1 WD |
116 | static boot_os_Fcn do_bootm_linux; |
117 | #else | |
118 | extern boot_os_Fcn do_bootm_linux; | |
119 | #endif | |
f72da340 WD |
120 | #ifdef CONFIG_SILENT_CONSOLE |
121 | static void fixup_silent_linux (void); | |
122 | #endif | |
47d1a6e1 | 123 | static boot_os_Fcn do_bootm_netbsd; |
d791b1dc | 124 | static boot_os_Fcn do_bootm_rtems; |
47d1a6e1 WD |
125 | #if (CONFIG_COMMANDS & CFG_CMD_ELF) |
126 | static boot_os_Fcn do_bootm_vxworks; | |
127 | static boot_os_Fcn do_bootm_qnxelf; | |
128 | int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] ); | |
129 | int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] ); | |
130 | #endif /* CFG_CMD_ELF */ | |
7f70e853 WD |
131 | #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC) |
132 | static boot_os_Fcn do_bootm_artos; | |
133 | #endif | |
1f4bb37d WD |
134 | #ifdef CONFIG_LYNXKDI |
135 | static boot_os_Fcn do_bootm_lynxkdi; | |
136 | extern void lynxkdi_boot( image_header_t * ); | |
137 | #endif | |
47d1a6e1 WD |
138 | |
139 | image_header_t header; | |
140 | ||
141 | ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */ | |
142 | ||
143 | int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
144 | { | |
145 | ulong iflag; | |
146 | ulong addr; | |
147 | ulong data, len, checksum; | |
148 | ulong *len_ptr; | |
c29fdfc1 | 149 | uint unc_len = 0x400000; |
47d1a6e1 WD |
150 | int i, verify; |
151 | char *name, *s; | |
b13fb01a | 152 | int (*appl)(int, char *[]); |
47d1a6e1 WD |
153 | image_header_t *hdr = &header; |
154 | ||
155 | s = getenv ("verify"); | |
156 | verify = (s && (*s == 'n')) ? 0 : 1; | |
157 | ||
158 | if (argc < 2) { | |
159 | addr = load_addr; | |
160 | } else { | |
161 | addr = simple_strtoul(argv[1], NULL, 16); | |
162 | } | |
163 | ||
164 | SHOW_BOOT_PROGRESS (1); | |
165 | printf ("## Booting image at %08lx ...\n", addr); | |
166 | ||
167 | /* Copy header so we can blank CRC field for re-calculation */ | |
2abbe075 WD |
168 | #ifdef CONFIG_HAS_DATAFLASH |
169 | if (addr_dataflash(addr)){ | |
170 | read_dataflash(addr, sizeof(image_header_t), (char *)&header); | |
171 | } else | |
172 | #endif | |
47d1a6e1 WD |
173 | memmove (&header, (char *)addr, sizeof(image_header_t)); |
174 | ||
175 | if (ntohl(hdr->ih_magic) != IH_MAGIC) { | |
2262cfee WD |
176 | #ifdef __I386__ /* correct image format not implemented yet - fake it */ |
177 | if (fake_header(hdr, (void*)addr, -1) != NULL) { | |
178 | /* to compensate for the addition below */ | |
179 | addr -= sizeof(image_header_t); | |
180 | /* turnof verify, | |
181 | * fake_header() does not fake the data crc | |
182 | */ | |
183 | verify = 0; | |
184 | } else | |
185 | #endif /* __I386__ */ | |
186 | { | |
4b9206ed | 187 | puts ("Bad Magic Number\n"); |
47d1a6e1 WD |
188 | SHOW_BOOT_PROGRESS (-1); |
189 | return 1; | |
2262cfee | 190 | } |
47d1a6e1 WD |
191 | } |
192 | SHOW_BOOT_PROGRESS (2); | |
193 | ||
194 | data = (ulong)&header; | |
195 | len = sizeof(image_header_t); | |
196 | ||
197 | checksum = ntohl(hdr->ih_hcrc); | |
198 | hdr->ih_hcrc = 0; | |
199 | ||
200 | if (crc32 (0, (char *)data, len) != checksum) { | |
4b9206ed | 201 | puts ("Bad Header Checksum\n"); |
47d1a6e1 WD |
202 | SHOW_BOOT_PROGRESS (-2); |
203 | return 1; | |
204 | } | |
205 | SHOW_BOOT_PROGRESS (3); | |
206 | ||
207 | /* for multi-file images we need the data part, too */ | |
a57a496f | 208 | print_image_hdr ((image_header_t *)addr); |
47d1a6e1 WD |
209 | |
210 | data = addr + sizeof(image_header_t); | |
211 | len = ntohl(hdr->ih_size); | |
212 | ||
2abbe075 WD |
213 | #ifdef CONFIG_HAS_DATAFLASH |
214 | if (addr_dataflash(addr)){ | |
215 | read_dataflash(data, len, (char *)CFG_LOAD_ADDR); | |
216 | data = CFG_LOAD_ADDR; | |
217 | } | |
8bde7f77 | 218 | #endif |
2abbe075 | 219 | |
47d1a6e1 | 220 | if (verify) { |
4b9206ed | 221 | puts (" Verifying Checksum ... "); |
47d1a6e1 WD |
222 | if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) { |
223 | printf ("Bad Data CRC\n"); | |
224 | SHOW_BOOT_PROGRESS (-3); | |
225 | return 1; | |
226 | } | |
4b9206ed | 227 | puts ("OK\n"); |
47d1a6e1 WD |
228 | } |
229 | SHOW_BOOT_PROGRESS (4); | |
230 | ||
231 | len_ptr = (ulong *)data; | |
232 | ||
2262cfee WD |
233 | #if defined(__PPC__) |
234 | if (hdr->ih_arch != IH_CPU_PPC) | |
235 | #elif defined(__ARM__) | |
236 | if (hdr->ih_arch != IH_CPU_ARM) | |
237 | #elif defined(__I386__) | |
238 | if (hdr->ih_arch != IH_CPU_I386) | |
6069ff26 | 239 | #elif defined(__mips__) |
8bde7f77 | 240 | if (hdr->ih_arch != IH_CPU_MIPS) |
4a551709 WD |
241 | #elif defined(__nios__) |
242 | if (hdr->ih_arch != IH_CPU_NIOS) | |
4e5ca3eb WD |
243 | #elif defined(__M68K__) |
244 | if (hdr->ih_arch != IH_CPU_M68K) | |
507bbe3e WD |
245 | #elif defined(__microblaze__) |
246 | if (hdr->ih_arch != IH_CPU_MICROBLAZE) | |
5c952cf0 WD |
247 | #elif defined(__nios2__) |
248 | if (hdr->ih_arch != IH_CPU_NIOS2) | |
2262cfee WD |
249 | #else |
250 | # error Unknown CPU type | |
251 | #endif | |
252 | { | |
253 | printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch); | |
47d1a6e1 WD |
254 | SHOW_BOOT_PROGRESS (-4); |
255 | return 1; | |
256 | } | |
257 | SHOW_BOOT_PROGRESS (5); | |
258 | ||
259 | switch (hdr->ih_type) { | |
b13fb01a WD |
260 | case IH_TYPE_STANDALONE: |
261 | name = "Standalone Application"; | |
262 | /* A second argument overwrites the load address */ | |
263 | if (argc > 2) { | |
264 | hdr->ih_load = simple_strtoul(argv[2], NULL, 16); | |
265 | } | |
266 | break; | |
267 | case IH_TYPE_KERNEL: | |
268 | name = "Kernel Image"; | |
269 | break; | |
d4ca31c4 | 270 | case IH_TYPE_MULTI: |
b13fb01a WD |
271 | name = "Multi-File Image"; |
272 | len = ntohl(len_ptr[0]); | |
273 | /* OS kernel is always the first image */ | |
274 | data += 8; /* kernel_len + terminator */ | |
275 | for (i=1; len_ptr[i]; ++i) | |
276 | data += 4; | |
277 | break; | |
47d1a6e1 WD |
278 | default: printf ("Wrong Image Type for %s command\n", cmdtp->name); |
279 | SHOW_BOOT_PROGRESS (-5); | |
280 | return 1; | |
281 | } | |
282 | SHOW_BOOT_PROGRESS (6); | |
283 | ||
284 | /* | |
285 | * We have reached the point of no return: we are going to | |
286 | * overwrite all exception vector code, so we cannot easily | |
287 | * recover from any failures any more... | |
288 | */ | |
289 | ||
290 | iflag = disable_interrupts(); | |
291 | ||
c7de829c WD |
292 | #ifdef CONFIG_AMIGAONEG3SE |
293 | /* | |
8bde7f77 | 294 | * We've possible left the caches enabled during |
c7de829c WD |
295 | * bios emulation, so turn them off again |
296 | */ | |
297 | icache_disable(); | |
298 | invalidate_l1_instruction_cache(); | |
299 | flush_data_cache(); | |
300 | dcache_disable(); | |
301 | #endif | |
302 | ||
47d1a6e1 WD |
303 | switch (hdr->ih_comp) { |
304 | case IH_COMP_NONE: | |
2262cfee | 305 | if(ntohl(hdr->ih_load) == addr) { |
47d1a6e1 WD |
306 | printf (" XIP %s ... ", name); |
307 | } else { | |
308 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
309 | size_t l = len; | |
310 | void *to = (void *)ntohl(hdr->ih_load); | |
311 | void *from = (void *)data; | |
312 | ||
313 | printf (" Loading %s ... ", name); | |
314 | ||
315 | while (l > 0) { | |
316 | size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l; | |
317 | WATCHDOG_RESET(); | |
318 | memmove (to, from, tail); | |
319 | to += tail; | |
320 | from += tail; | |
321 | l -= tail; | |
322 | } | |
323 | #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ | |
324 | memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len); | |
325 | #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ | |
326 | } | |
327 | break; | |
328 | case IH_COMP_GZIP: | |
329 | printf (" Uncompressing %s ... ", name); | |
c29fdfc1 | 330 | if (gunzip ((void *)ntohl(hdr->ih_load), unc_len, |
eedcd078 | 331 | (uchar *)data, &len) != 0) { |
4b9206ed | 332 | puts ("GUNZIP ERROR - must RESET board to recover\n"); |
47d1a6e1 WD |
333 | SHOW_BOOT_PROGRESS (-6); |
334 | do_reset (cmdtp, flag, argc, argv); | |
335 | } | |
336 | break; | |
c29fdfc1 WD |
337 | #ifdef CONFIG_BZIP2 |
338 | case IH_COMP_BZIP2: | |
339 | printf (" Uncompressing %s ... ", name); | |
5653fc33 WD |
340 | /* |
341 | * If we've got less than 4 MB of malloc() space, | |
342 | * use slower decompression algorithm which requires | |
343 | * at most 2300 KB of memory. | |
344 | */ | |
c29fdfc1 | 345 | i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load), |
5653fc33 WD |
346 | &unc_len, (char *)data, len, |
347 | CFG_MALLOC_LEN < (4096 * 1024), 0); | |
c29fdfc1 WD |
348 | if (i != BZ_OK) { |
349 | printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i); | |
350 | SHOW_BOOT_PROGRESS (-6); | |
351 | udelay(100000); | |
352 | do_reset (cmdtp, flag, argc, argv); | |
353 | } | |
354 | break; | |
355 | #endif /* CONFIG_BZIP2 */ | |
47d1a6e1 WD |
356 | default: |
357 | if (iflag) | |
358 | enable_interrupts(); | |
359 | printf ("Unimplemented compression type %d\n", hdr->ih_comp); | |
360 | SHOW_BOOT_PROGRESS (-7); | |
361 | return 1; | |
362 | } | |
4b9206ed | 363 | puts ("OK\n"); |
47d1a6e1 WD |
364 | SHOW_BOOT_PROGRESS (7); |
365 | ||
366 | switch (hdr->ih_type) { | |
367 | case IH_TYPE_STANDALONE: | |
47d1a6e1 WD |
368 | if (iflag) |
369 | enable_interrupts(); | |
370 | ||
4a6fd34b WD |
371 | /* load (and uncompress), but don't start if "autostart" |
372 | * is set to "no" | |
373 | */ | |
4a551709 WD |
374 | if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) { |
375 | char buf[32]; | |
376 | sprintf(buf, "%lX", len); | |
377 | setenv("filesize", buf); | |
4a6fd34b | 378 | return 0; |
4a551709 | 379 | } |
b13fb01a WD |
380 | appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep); |
381 | (*appl)(argc-1, &argv[1]); | |
4a6fd34b | 382 | return 0; |
47d1a6e1 WD |
383 | case IH_TYPE_KERNEL: |
384 | case IH_TYPE_MULTI: | |
385 | /* handled below */ | |
386 | break; | |
387 | default: | |
388 | if (iflag) | |
389 | enable_interrupts(); | |
390 | printf ("Can't boot image type %d\n", hdr->ih_type); | |
391 | SHOW_BOOT_PROGRESS (-8); | |
392 | return 1; | |
393 | } | |
394 | SHOW_BOOT_PROGRESS (8); | |
395 | ||
396 | switch (hdr->ih_os) { | |
397 | default: /* handled by (original) Linux case */ | |
398 | case IH_OS_LINUX: | |
f72da340 WD |
399 | #ifdef CONFIG_SILENT_CONSOLE |
400 | fixup_silent_linux(); | |
401 | #endif | |
47d1a6e1 WD |
402 | do_bootm_linux (cmdtp, flag, argc, argv, |
403 | addr, len_ptr, verify); | |
404 | break; | |
405 | case IH_OS_NETBSD: | |
406 | do_bootm_netbsd (cmdtp, flag, argc, argv, | |
407 | addr, len_ptr, verify); | |
408 | break; | |
8bde7f77 | 409 | |
1f4bb37d WD |
410 | #ifdef CONFIG_LYNXKDI |
411 | case IH_OS_LYNXOS: | |
412 | do_bootm_lynxkdi (cmdtp, flag, argc, argv, | |
413 | addr, len_ptr, verify); | |
414 | break; | |
415 | #endif | |
416 | ||
d791b1dc WD |
417 | case IH_OS_RTEMS: |
418 | do_bootm_rtems (cmdtp, flag, argc, argv, | |
419 | addr, len_ptr, verify); | |
420 | break; | |
8bde7f77 | 421 | |
47d1a6e1 WD |
422 | #if (CONFIG_COMMANDS & CFG_CMD_ELF) |
423 | case IH_OS_VXWORKS: | |
424 | do_bootm_vxworks (cmdtp, flag, argc, argv, | |
425 | addr, len_ptr, verify); | |
426 | break; | |
427 | case IH_OS_QNX: | |
428 | do_bootm_qnxelf (cmdtp, flag, argc, argv, | |
429 | addr, len_ptr, verify); | |
430 | break; | |
431 | #endif /* CFG_CMD_ELF */ | |
7f70e853 WD |
432 | #ifdef CONFIG_ARTOS |
433 | case IH_OS_ARTOS: | |
434 | do_bootm_artos (cmdtp, flag, argc, argv, | |
435 | addr, len_ptr, verify); | |
436 | break; | |
437 | #endif | |
47d1a6e1 WD |
438 | } |
439 | ||
440 | SHOW_BOOT_PROGRESS (-9); | |
441 | #ifdef DEBUG | |
4b9206ed | 442 | puts ("\n## Control returned to monitor - resetting...\n"); |
47d1a6e1 WD |
443 | do_reset (cmdtp, flag, argc, argv); |
444 | #endif | |
445 | return 1; | |
446 | } | |
447 | ||
0d498393 WD |
448 | U_BOOT_CMD( |
449 | bootm, CFG_MAXARGS, 1, do_bootm, | |
8bde7f77 WD |
450 | "bootm - boot application image from memory\n", |
451 | "[addr [arg ...]]\n - boot application image stored in memory\n" | |
452 | " passing arguments 'arg ...'; when booting a Linux kernel,\n" | |
453 | " 'arg' can be the address of an initrd image\n" | |
454 | ); | |
455 | ||
f72da340 WD |
456 | #ifdef CONFIG_SILENT_CONSOLE |
457 | static void | |
458 | fixup_silent_linux () | |
459 | { | |
460 | DECLARE_GLOBAL_DATA_PTR; | |
461 | char buf[256], *start, *end; | |
462 | char *cmdline = getenv ("bootargs"); | |
463 | ||
464 | /* Only fix cmdline when requested */ | |
465 | if (!(gd->flags & GD_FLG_SILENT)) | |
466 | return; | |
467 | ||
468 | debug ("before silent fix-up: %s\n", cmdline); | |
469 | if (cmdline) { | |
470 | if ((start = strstr (cmdline, "console=")) != NULL) { | |
471 | end = strchr (start, ' '); | |
472 | strncpy (buf, cmdline, (start - cmdline + 8)); | |
473 | if (end) | |
474 | strcpy (buf + (start - cmdline + 8), end); | |
475 | else | |
476 | buf[start - cmdline + 8] = '\0'; | |
477 | } else { | |
478 | strcpy (buf, cmdline); | |
479 | strcat (buf, " console="); | |
480 | } | |
481 | } else { | |
482 | strcpy (buf, "console="); | |
483 | } | |
484 | ||
485 | setenv ("bootargs", buf); | |
486 | debug ("after silent fix-up: %s\n", buf); | |
487 | } | |
488 | #endif /* CONFIG_SILENT_CONSOLE */ | |
489 | ||
2262cfee | 490 | #ifdef CONFIG_PPC |
47d1a6e1 WD |
491 | static void |
492 | do_bootm_linux (cmd_tbl_t *cmdtp, int flag, | |
493 | int argc, char *argv[], | |
494 | ulong addr, | |
495 | ulong *len_ptr, | |
496 | int verify) | |
497 | { | |
498 | DECLARE_GLOBAL_DATA_PTR; | |
499 | ||
500 | ulong sp; | |
501 | ulong len, checksum; | |
502 | ulong initrd_start, initrd_end; | |
503 | ulong cmd_start, cmd_end; | |
504 | ulong initrd_high; | |
505 | ulong data; | |
38b99261 | 506 | int initrd_copy_to_ram = 1; |
47d1a6e1 WD |
507 | char *cmdline; |
508 | char *s; | |
509 | bd_t *kbd; | |
510 | void (*kernel)(bd_t *, ulong, ulong, ulong, ulong); | |
511 | image_header_t *hdr = &header; | |
512 | ||
513 | if ((s = getenv ("initrd_high")) != NULL) { | |
514 | /* a value of "no" or a similar string will act like 0, | |
515 | * turning the "load high" feature off. This is intentional. | |
516 | */ | |
517 | initrd_high = simple_strtoul(s, NULL, 16); | |
38b99261 WD |
518 | if (initrd_high == ~0) |
519 | initrd_copy_to_ram = 0; | |
228f29ac | 520 | } else { /* not set, no restrictions to load high */ |
47d1a6e1 WD |
521 | initrd_high = ~0; |
522 | } | |
523 | ||
56f94be3 | 524 | #ifdef CONFIG_LOGBUFFER |
6aff3115 | 525 | kbd=gd->bd; |
228f29ac WD |
526 | /* Prevent initrd from overwriting logbuffer */ |
527 | if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD)) | |
528 | initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD; | |
529 | debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN); | |
56f94be3 WD |
530 | #endif |
531 | ||
47d1a6e1 WD |
532 | /* |
533 | * Booting a (Linux) kernel image | |
534 | * | |
535 | * Allocate space for command line and board info - the | |
536 | * address should be as high as possible within the reach of | |
537 | * the kernel (see CFG_BOOTMAPSZ settings), but in unused | |
538 | * memory, which means far enough below the current stack | |
539 | * pointer. | |
540 | */ | |
541 | ||
542 | asm( "mr %0,1": "=r"(sp) : ); | |
543 | ||
56f94be3 WD |
544 | debug ("## Current stack ends at 0x%08lX ", sp); |
545 | ||
47d1a6e1 WD |
546 | sp -= 2048; /* just to be sure */ |
547 | if (sp > CFG_BOOTMAPSZ) | |
548 | sp = CFG_BOOTMAPSZ; | |
549 | sp &= ~0xF; | |
550 | ||
56f94be3 WD |
551 | debug ("=> set upper limit to 0x%08lX\n", sp); |
552 | ||
47d1a6e1 WD |
553 | cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF); |
554 | kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF); | |
555 | ||
556 | if ((s = getenv("bootargs")) == NULL) | |
557 | s = ""; | |
558 | ||
559 | strcpy (cmdline, s); | |
560 | ||
561 | cmd_start = (ulong)&cmdline[0]; | |
562 | cmd_end = cmd_start + strlen(cmdline); | |
563 | ||
564 | *kbd = *(gd->bd); | |
565 | ||
566 | #ifdef DEBUG | |
567 | printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end); | |
568 | ||
569 | do_bdinfo (NULL, 0, 0, NULL); | |
570 | #endif | |
571 | ||
572 | if ((s = getenv ("clocks_in_mhz")) != NULL) { | |
573 | /* convert all clock information to MHz */ | |
574 | kbd->bi_intfreq /= 1000000L; | |
575 | kbd->bi_busfreq /= 1000000L; | |
42d1f039 | 576 | #if defined(CONFIG_8260) || defined(CONFIG_MPC8560) |
47d1a6e1 WD |
577 | kbd->bi_cpmfreq /= 1000000L; |
578 | kbd->bi_brgfreq /= 1000000L; | |
579 | kbd->bi_sccfreq /= 1000000L; | |
580 | kbd->bi_vco /= 1000000L; | |
581 | #endif /* CONFIG_8260 */ | |
cbd8a35c | 582 | #if defined(CONFIG_MPC5xxx) |
945af8d7 WD |
583 | kbd->bi_ipbfreq /= 1000000L; |
584 | kbd->bi_pcifreq /= 1000000L; | |
cbd8a35c | 585 | #endif /* CONFIG_MPC5xxx */ |
47d1a6e1 WD |
586 | } |
587 | ||
588 | kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep; | |
589 | ||
590 | /* | |
591 | * Check if there is an initrd image | |
592 | */ | |
593 | if (argc >= 3) { | |
594 | SHOW_BOOT_PROGRESS (9); | |
595 | ||
596 | addr = simple_strtoul(argv[2], NULL, 16); | |
597 | ||
598 | printf ("## Loading RAMDisk Image at %08lx ...\n", addr); | |
599 | ||
600 | /* Copy header so we can blank CRC field for re-calculation */ | |
601 | memmove (&header, (char *)addr, sizeof(image_header_t)); | |
602 | ||
603 | if (hdr->ih_magic != IH_MAGIC) { | |
4b9206ed | 604 | puts ("Bad Magic Number\n"); |
47d1a6e1 WD |
605 | SHOW_BOOT_PROGRESS (-10); |
606 | do_reset (cmdtp, flag, argc, argv); | |
607 | } | |
608 | ||
609 | data = (ulong)&header; | |
610 | len = sizeof(image_header_t); | |
611 | ||
612 | checksum = hdr->ih_hcrc; | |
613 | hdr->ih_hcrc = 0; | |
614 | ||
615 | if (crc32 (0, (char *)data, len) != checksum) { | |
4b9206ed | 616 | puts ("Bad Header Checksum\n"); |
47d1a6e1 WD |
617 | SHOW_BOOT_PROGRESS (-11); |
618 | do_reset (cmdtp, flag, argc, argv); | |
619 | } | |
620 | ||
621 | SHOW_BOOT_PROGRESS (10); | |
622 | ||
623 | print_image_hdr (hdr); | |
624 | ||
625 | data = addr + sizeof(image_header_t); | |
626 | len = hdr->ih_size; | |
627 | ||
628 | if (verify) { | |
629 | ulong csum = 0; | |
630 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
631 | ulong cdata = data, edata = cdata + len; | |
632 | #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ | |
633 | ||
4b9206ed | 634 | puts (" Verifying Checksum ... "); |
47d1a6e1 WD |
635 | |
636 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
637 | ||
638 | while (cdata < edata) { | |
639 | ulong chunk = edata - cdata; | |
640 | ||
641 | if (chunk > CHUNKSZ) | |
642 | chunk = CHUNKSZ; | |
643 | csum = crc32 (csum, (char *)cdata, chunk); | |
644 | cdata += chunk; | |
645 | ||
646 | WATCHDOG_RESET(); | |
647 | } | |
648 | #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ | |
649 | csum = crc32 (0, (char *)data, len); | |
650 | #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ | |
651 | ||
652 | if (csum != hdr->ih_dcrc) { | |
4b9206ed | 653 | puts ("Bad Data CRC\n"); |
47d1a6e1 WD |
654 | SHOW_BOOT_PROGRESS (-12); |
655 | do_reset (cmdtp, flag, argc, argv); | |
656 | } | |
4b9206ed | 657 | puts ("OK\n"); |
47d1a6e1 WD |
658 | } |
659 | ||
660 | SHOW_BOOT_PROGRESS (11); | |
661 | ||
662 | if ((hdr->ih_os != IH_OS_LINUX) || | |
663 | (hdr->ih_arch != IH_CPU_PPC) || | |
664 | (hdr->ih_type != IH_TYPE_RAMDISK) ) { | |
4b9206ed | 665 | puts ("No Linux PPC Ramdisk Image\n"); |
47d1a6e1 WD |
666 | SHOW_BOOT_PROGRESS (-13); |
667 | do_reset (cmdtp, flag, argc, argv); | |
668 | } | |
669 | ||
670 | /* | |
671 | * Now check if we have a multifile image | |
672 | */ | |
673 | } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) { | |
674 | u_long tail = ntohl(len_ptr[0]) % 4; | |
675 | int i; | |
676 | ||
677 | SHOW_BOOT_PROGRESS (13); | |
678 | ||
679 | /* skip kernel length and terminator */ | |
680 | data = (ulong)(&len_ptr[2]); | |
681 | /* skip any additional image length fields */ | |
682 | for (i=1; len_ptr[i]; ++i) | |
683 | data += 4; | |
684 | /* add kernel length, and align */ | |
685 | data += ntohl(len_ptr[0]); | |
686 | if (tail) { | |
687 | data += 4 - tail; | |
688 | } | |
689 | ||
690 | len = ntohl(len_ptr[1]); | |
691 | ||
692 | } else { | |
693 | /* | |
694 | * no initrd image | |
695 | */ | |
696 | SHOW_BOOT_PROGRESS (14); | |
697 | ||
698 | len = data = 0; | |
699 | } | |
700 | ||
47d1a6e1 | 701 | if (!data) { |
56f94be3 | 702 | debug ("No initrd\n"); |
47d1a6e1 | 703 | } |
47d1a6e1 WD |
704 | |
705 | if (data) { | |
38b99261 WD |
706 | if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ |
707 | initrd_start = data; | |
708 | initrd_end = initrd_start + len; | |
709 | } else { | |
47d1a6e1 WD |
710 | initrd_start = (ulong)kbd - len; |
711 | initrd_start &= ~(4096 - 1); /* align on page */ | |
712 | ||
713 | if (initrd_high) { | |
714 | ulong nsp; | |
715 | ||
716 | /* | |
717 | * the inital ramdisk does not need to be within | |
718 | * CFG_BOOTMAPSZ as it is not accessed until after | |
719 | * the mm system is initialised. | |
720 | * | |
721 | * do the stack bottom calculation again and see if | |
722 | * the initrd will fit just below the monitor stack | |
723 | * bottom without overwriting the area allocated | |
724 | * above for command line args and board info. | |
725 | */ | |
726 | asm( "mr %0,1": "=r"(nsp) : ); | |
727 | nsp -= 2048; /* just to be sure */ | |
728 | nsp &= ~0xF; | |
729 | if (nsp > initrd_high) /* limit as specified */ | |
730 | nsp = initrd_high; | |
731 | nsp -= len; | |
732 | nsp &= ~(4096 - 1); /* align on page */ | |
733 | if (nsp >= sp) | |
734 | initrd_start = nsp; | |
735 | } | |
736 | ||
737 | SHOW_BOOT_PROGRESS (12); | |
56f94be3 WD |
738 | |
739 | debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n", | |
47d1a6e1 | 740 | data, data + len - 1, len, len); |
56f94be3 | 741 | |
47d1a6e1 WD |
742 | initrd_end = initrd_start + len; |
743 | printf (" Loading Ramdisk to %08lx, end %08lx ... ", | |
744 | initrd_start, initrd_end); | |
745 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
746 | { | |
747 | size_t l = len; | |
748 | void *to = (void *)initrd_start; | |
749 | void *from = (void *)data; | |
750 | ||
751 | while (l > 0) { | |
752 | size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l; | |
753 | WATCHDOG_RESET(); | |
754 | memmove (to, from, tail); | |
755 | to += tail; | |
756 | from += tail; | |
757 | l -= tail; | |
758 | } | |
759 | } | |
760 | #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */ | |
761 | memmove ((void *)initrd_start, (void *)data, len); | |
762 | #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */ | |
4b9206ed | 763 | puts ("OK\n"); |
38b99261 | 764 | } |
47d1a6e1 WD |
765 | } else { |
766 | initrd_start = 0; | |
767 | initrd_end = 0; | |
768 | } | |
769 | ||
56f94be3 WD |
770 | |
771 | debug ("## Transferring control to Linux (at address %08lx) ...\n", | |
47d1a6e1 | 772 | (ulong)kernel); |
56f94be3 | 773 | |
47d1a6e1 WD |
774 | SHOW_BOOT_PROGRESS (15); |
775 | ||
42d1f039 | 776 | #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500) |
47d1a6e1 WD |
777 | unlock_ram_in_cache(); |
778 | #endif | |
779 | /* | |
780 | * Linux Kernel Parameters: | |
781 | * r3: ptr to board info data | |
782 | * r4: initrd_start or 0 if no initrd | |
783 | * r5: initrd_end - unused if r4 is 0 | |
784 | * r6: Start of command line string | |
785 | * r7: End of command line string | |
786 | */ | |
787 | (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end); | |
788 | } | |
1cb8e980 | 789 | #endif /* CONFIG_PPC */ |
47d1a6e1 WD |
790 | |
791 | static void | |
792 | do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag, | |
793 | int argc, char *argv[], | |
794 | ulong addr, | |
795 | ulong *len_ptr, | |
796 | int verify) | |
797 | { | |
798 | DECLARE_GLOBAL_DATA_PTR; | |
799 | ||
800 | image_header_t *hdr = &header; | |
801 | ||
802 | void (*loader)(bd_t *, image_header_t *, char *, char *); | |
803 | image_header_t *img_addr; | |
804 | char *consdev; | |
805 | char *cmdline; | |
806 | ||
807 | ||
808 | /* | |
809 | * Booting a (NetBSD) kernel image | |
810 | * | |
811 | * This process is pretty similar to a standalone application: | |
812 | * The (first part of an multi-) image must be a stage-2 loader, | |
813 | * which in turn is responsible for loading & invoking the actual | |
814 | * kernel. The only differences are the parameters being passed: | |
815 | * besides the board info strucure, the loader expects a command | |
816 | * line, the name of the console device, and (optionally) the | |
817 | * address of the original image header. | |
818 | */ | |
819 | ||
820 | img_addr = 0; | |
821 | if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) | |
822 | img_addr = (image_header_t *) addr; | |
823 | ||
824 | ||
825 | consdev = ""; | |
826 | #if defined (CONFIG_8xx_CONS_SMC1) | |
827 | consdev = "smc1"; | |
828 | #elif defined (CONFIG_8xx_CONS_SMC2) | |
829 | consdev = "smc2"; | |
830 | #elif defined (CONFIG_8xx_CONS_SCC2) | |
831 | consdev = "scc2"; | |
832 | #elif defined (CONFIG_8xx_CONS_SCC3) | |
833 | consdev = "scc3"; | |
834 | #endif | |
835 | ||
836 | if (argc > 2) { | |
837 | ulong len; | |
838 | int i; | |
839 | ||
840 | for (i=2, len=0 ; i<argc ; i+=1) | |
841 | len += strlen (argv[i]) + 1; | |
842 | cmdline = malloc (len); | |
843 | ||
844 | for (i=2, len=0 ; i<argc ; i+=1) { | |
845 | if (i > 2) | |
846 | cmdline[len++] = ' '; | |
847 | strcpy (&cmdline[len], argv[i]); | |
848 | len += strlen (argv[i]); | |
849 | } | |
850 | } else if ((cmdline = getenv("bootargs")) == NULL) { | |
851 | cmdline = ""; | |
852 | } | |
853 | ||
854 | loader = (void (*)(bd_t *, image_header_t *, char *, char *)) hdr->ih_ep; | |
855 | ||
856 | printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n", | |
857 | (ulong)loader); | |
858 | ||
859 | SHOW_BOOT_PROGRESS (15); | |
860 | ||
861 | /* | |
862 | * NetBSD Stage-2 Loader Parameters: | |
863 | * r3: ptr to board info data | |
864 | * r4: image address | |
865 | * r5: console device | |
866 | * r6: boot args string | |
867 | */ | |
868 | (*loader) (gd->bd, img_addr, consdev, cmdline); | |
869 | } | |
870 | ||
7f70e853 WD |
871 | #if defined(CONFIG_ARTOS) && defined(CONFIG_PPC) |
872 | ||
873 | /* Function that returns a character from the environment */ | |
874 | extern uchar (*env_get_char)(int); | |
875 | ||
876 | static void | |
877 | do_bootm_artos (cmd_tbl_t *cmdtp, int flag, | |
878 | int argc, char *argv[], | |
879 | ulong addr, | |
880 | ulong *len_ptr, | |
881 | int verify) | |
882 | { | |
883 | DECLARE_GLOBAL_DATA_PTR; | |
884 | ulong top; | |
885 | char *s, *cmdline; | |
886 | char **fwenv, **ss; | |
887 | int i, j, nxt, len, envno, envsz; | |
888 | bd_t *kbd; | |
889 | void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top); | |
890 | image_header_t *hdr = &header; | |
891 | ||
892 | /* | |
893 | * Booting an ARTOS kernel image + application | |
894 | */ | |
895 | ||
896 | /* this used to be the top of memory, but was wrong... */ | |
897 | #ifdef CONFIG_PPC | |
898 | /* get stack pointer */ | |
899 | asm volatile ("mr %0,1" : "=r"(top) ); | |
900 | #endif | |
901 | debug ("## Current stack ends at 0x%08lX ", top); | |
902 | ||
903 | top -= 2048; /* just to be sure */ | |
904 | if (top > CFG_BOOTMAPSZ) | |
905 | top = CFG_BOOTMAPSZ; | |
906 | top &= ~0xF; | |
907 | ||
908 | debug ("=> set upper limit to 0x%08lX\n", top); | |
909 | ||
910 | /* first check the artos specific boot args, then the linux args*/ | |
911 | if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL) | |
912 | s = ""; | |
913 | ||
914 | /* get length of cmdline, and place it */ | |
915 | len = strlen(s); | |
916 | top = (top - (len + 1)) & ~0xF; | |
917 | cmdline = (char *)top; | |
918 | debug ("## cmdline at 0x%08lX ", top); | |
919 | strcpy(cmdline, s); | |
920 | ||
921 | /* copy bdinfo */ | |
922 | top = (top - sizeof(bd_t)) & ~0xF; | |
923 | debug ("## bd at 0x%08lX ", top); | |
924 | kbd = (bd_t *)top; | |
925 | memcpy(kbd, gd->bd, sizeof(bd_t)); | |
926 | ||
927 | /* first find number of env entries, and their size */ | |
928 | envno = 0; | |
929 | envsz = 0; | |
930 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { | |
931 | for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) | |
932 | ; | |
933 | envno++; | |
934 | envsz += (nxt - i) + 1; /* plus trailing zero */ | |
935 | } | |
936 | envno++; /* plus the terminating zero */ | |
937 | debug ("## %u envvars total size %u ", envno, envsz); | |
938 | ||
939 | top = (top - sizeof(char **)*envno) & ~0xF; | |
940 | fwenv = (char **)top; | |
941 | debug ("## fwenv at 0x%08lX ", top); | |
942 | ||
943 | top = (top - envsz) & ~0xF; | |
944 | s = (char *)top; | |
945 | ss = fwenv; | |
946 | ||
947 | /* now copy them */ | |
948 | for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) { | |
949 | for (nxt = i; env_get_char(nxt) != '\0'; ++nxt) | |
950 | ; | |
951 | *ss++ = s; | |
952 | for (j = i; j < nxt; ++j) | |
953 | *s++ = env_get_char(j); | |
954 | *s++ = '\0'; | |
955 | } | |
956 | *ss++ = NULL; /* terminate */ | |
957 | ||
958 | entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep); | |
959 | (*entry)(kbd, cmdline, fwenv, top); | |
960 | } | |
961 | #endif | |
962 | ||
963 | ||
47d1a6e1 WD |
964 | #if (CONFIG_COMMANDS & CFG_CMD_BOOTD) |
965 | int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
966 | { | |
967 | int rcode = 0; | |
968 | #ifndef CFG_HUSH_PARSER | |
969 | if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1; | |
970 | #else | |
971 | if (parse_string_outer(getenv("bootcmd"), | |
972 | FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1; | |
973 | #endif | |
974 | return rcode; | |
975 | } | |
8bde7f77 | 976 | |
0d498393 WD |
977 | U_BOOT_CMD( |
978 | boot, 1, 1, do_bootd, | |
9d2b18a0 WD |
979 | "boot - boot default, i.e., run 'bootcmd'\n", |
980 | NULL | |
981 | ); | |
982 | ||
983 | /* keep old command name "bootd" for backward compatibility */ | |
0d498393 WD |
984 | U_BOOT_CMD( |
985 | bootd, 1, 1, do_bootd, | |
8bde7f77 WD |
986 | "bootd - boot default, i.e., run 'bootcmd'\n", |
987 | NULL | |
988 | ); | |
989 | ||
47d1a6e1 WD |
990 | #endif |
991 | ||
992 | #if (CONFIG_COMMANDS & CFG_CMD_IMI) | |
993 | int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
994 | { | |
995 | int arg; | |
996 | ulong addr; | |
997 | int rcode=0; | |
998 | ||
999 | if (argc < 2) { | |
1000 | return image_info (load_addr); | |
1001 | } | |
1002 | ||
1003 | for (arg=1; arg <argc; ++arg) { | |
1004 | addr = simple_strtoul(argv[arg], NULL, 16); | |
1005 | if (image_info (addr) != 0) rcode = 1; | |
1006 | } | |
1007 | return rcode; | |
1008 | } | |
1009 | ||
1010 | static int image_info (ulong addr) | |
1011 | { | |
1012 | ulong data, len, checksum; | |
1013 | image_header_t *hdr = &header; | |
1014 | ||
1015 | printf ("\n## Checking Image at %08lx ...\n", addr); | |
1016 | ||
1017 | /* Copy header so we can blank CRC field for re-calculation */ | |
1018 | memmove (&header, (char *)addr, sizeof(image_header_t)); | |
1019 | ||
1020 | if (ntohl(hdr->ih_magic) != IH_MAGIC) { | |
4b9206ed | 1021 | puts (" Bad Magic Number\n"); |
47d1a6e1 WD |
1022 | return 1; |
1023 | } | |
1024 | ||
1025 | data = (ulong)&header; | |
1026 | len = sizeof(image_header_t); | |
1027 | ||
1028 | checksum = ntohl(hdr->ih_hcrc); | |
1029 | hdr->ih_hcrc = 0; | |
1030 | ||
1031 | if (crc32 (0, (char *)data, len) != checksum) { | |
4b9206ed | 1032 | puts (" Bad Header Checksum\n"); |
47d1a6e1 WD |
1033 | return 1; |
1034 | } | |
1035 | ||
1036 | /* for multi-file images we need the data part, too */ | |
1037 | print_image_hdr ((image_header_t *)addr); | |
1038 | ||
1039 | data = addr + sizeof(image_header_t); | |
1040 | len = ntohl(hdr->ih_size); | |
1041 | ||
4b9206ed | 1042 | puts (" Verifying Checksum ... "); |
47d1a6e1 | 1043 | if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) { |
4b9206ed | 1044 | puts (" Bad Data CRC\n"); |
47d1a6e1 WD |
1045 | return 1; |
1046 | } | |
4b9206ed | 1047 | puts ("OK\n"); |
47d1a6e1 WD |
1048 | return 0; |
1049 | } | |
0d498393 WD |
1050 | |
1051 | U_BOOT_CMD( | |
1052 | iminfo, CFG_MAXARGS, 1, do_iminfo, | |
8bde7f77 WD |
1053 | "iminfo - print header information for application image\n", |
1054 | "addr [addr ...]\n" | |
1055 | " - print header information for application image starting at\n" | |
1056 | " address 'addr' in memory; this includes verification of the\n" | |
1057 | " image contents (magic number, header and payload checksums)\n" | |
1058 | ); | |
1059 | ||
47d1a6e1 WD |
1060 | #endif /* CFG_CMD_IMI */ |
1061 | ||
27b207fd WD |
1062 | #if (CONFIG_COMMANDS & CFG_CMD_IMLS) |
1063 | /*----------------------------------------------------------------------- | |
1064 | * List all images found in flash. | |
1065 | */ | |
1066 | int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) | |
1067 | { | |
1068 | flash_info_t *info; | |
1069 | int i, j; | |
1070 | image_header_t *hdr; | |
5bb226e8 | 1071 | ulong data, len, checksum; |
27b207fd WD |
1072 | |
1073 | for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) { | |
1074 | if (info->flash_id == FLASH_UNKNOWN) | |
1075 | goto next_bank; | |
1076 | for (j=0; j<CFG_MAX_FLASH_SECT; ++j) { | |
1077 | ||
1078 | if (!(hdr=(image_header_t *)info->start[j]) || | |
1079 | (ntohl(hdr->ih_magic) != IH_MAGIC)) | |
1080 | goto next_sector; | |
1081 | ||
1082 | /* Copy header so we can blank CRC field for re-calculation */ | |
1083 | memmove (&header, (char *)hdr, sizeof(image_header_t)); | |
1084 | ||
1085 | checksum = ntohl(header.ih_hcrc); | |
1086 | header.ih_hcrc = 0; | |
1087 | ||
1088 | if (crc32 (0, (char *)&header, sizeof(image_header_t)) | |
1089 | != checksum) | |
1090 | goto next_sector; | |
1091 | ||
1092 | printf ("Image at %08lX:\n", (ulong)hdr); | |
1093 | print_image_hdr( hdr ); | |
5bb226e8 WD |
1094 | |
1095 | data = (ulong)hdr + sizeof(image_header_t); | |
1096 | len = ntohl(hdr->ih_size); | |
1097 | ||
4b9206ed | 1098 | puts (" Verifying Checksum ... "); |
5bb226e8 | 1099 | if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) { |
4b9206ed | 1100 | puts (" Bad Data CRC\n"); |
5bb226e8 | 1101 | } |
4b9206ed | 1102 | puts ("OK\n"); |
bdccc4fe | 1103 | next_sector: ; |
27b207fd | 1104 | } |
bdccc4fe | 1105 | next_bank: ; |
27b207fd WD |
1106 | } |
1107 | ||
1108 | return (0); | |
1109 | } | |
1110 | ||
1111 | U_BOOT_CMD( | |
1112 | imls, 1, 1, do_imls, | |
1113 | "imls - list all images found in flash\n", | |
1114 | "\n" | |
1115 | " - Prints information about all images found at sector\n" | |
1116 | " boundaries in flash.\n" | |
1117 | ); | |
1118 | #endif /* CFG_CMD_IMLS */ | |
1119 | ||
47d1a6e1 WD |
1120 | void |
1121 | print_image_hdr (image_header_t *hdr) | |
1122 | { | |
1123 | #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP) | |
1124 | time_t timestamp = (time_t)ntohl(hdr->ih_time); | |
1125 | struct rtc_time tm; | |
1126 | #endif | |
1127 | ||
1128 | printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name); | |
1129 | #if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP) | |
1130 | to_tm (timestamp, &tm); | |
1131 | printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n", | |
1132 | tm.tm_year, tm.tm_mon, tm.tm_mday, | |
1133 | tm.tm_hour, tm.tm_min, tm.tm_sec); | |
1134 | #endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */ | |
4b9206ed WD |
1135 | puts (" Image Type: "); print_type(hdr); |
1136 | printf ("\n Data Size: %d Bytes = ", ntohl(hdr->ih_size)); | |
47d1a6e1 | 1137 | print_size (ntohl(hdr->ih_size), "\n"); |
4b9206ed WD |
1138 | printf (" Load Address: %08x\n" |
1139 | " Entry Point: %08x\n", | |
1140 | ntohl(hdr->ih_load), ntohl(hdr->ih_ep)); | |
47d1a6e1 WD |
1141 | |
1142 | if (hdr->ih_type == IH_TYPE_MULTI) { | |
1143 | int i; | |
1144 | ulong len; | |
1145 | ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t)); | |
1146 | ||
4b9206ed | 1147 | puts (" Contents:\n"); |
47d1a6e1 WD |
1148 | for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) { |
1149 | printf (" Image %d: %8ld Bytes = ", i, len); | |
1150 | print_size (len, "\n"); | |
1151 | } | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | ||
1156 | static void | |
1157 | print_type (image_header_t *hdr) | |
1158 | { | |
1159 | char *os, *arch, *type, *comp; | |
1160 | ||
1161 | switch (hdr->ih_os) { | |
1162 | case IH_OS_INVALID: os = "Invalid OS"; break; | |
1163 | case IH_OS_NETBSD: os = "NetBSD"; break; | |
1164 | case IH_OS_LINUX: os = "Linux"; break; | |
1165 | case IH_OS_VXWORKS: os = "VxWorks"; break; | |
1166 | case IH_OS_QNX: os = "QNX"; break; | |
1167 | case IH_OS_U_BOOT: os = "U-Boot"; break; | |
d791b1dc | 1168 | case IH_OS_RTEMS: os = "RTEMS"; break; |
7f70e853 WD |
1169 | #ifdef CONFIG_ARTOS |
1170 | case IH_OS_ARTOS: os = "ARTOS"; break; | |
1f4bb37d WD |
1171 | #endif |
1172 | #ifdef CONFIG_LYNXKDI | |
1173 | case IH_OS_LYNXOS: os = "LynxOS"; break; | |
7f70e853 | 1174 | #endif |
47d1a6e1 WD |
1175 | default: os = "Unknown OS"; break; |
1176 | } | |
1177 | ||
1178 | switch (hdr->ih_arch) { | |
1179 | case IH_CPU_INVALID: arch = "Invalid CPU"; break; | |
1180 | case IH_CPU_ALPHA: arch = "Alpha"; break; | |
1181 | case IH_CPU_ARM: arch = "ARM"; break; | |
1182 | case IH_CPU_I386: arch = "Intel x86"; break; | |
1183 | case IH_CPU_IA64: arch = "IA64"; break; | |
1184 | case IH_CPU_MIPS: arch = "MIPS"; break; | |
1185 | case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break; | |
1186 | case IH_CPU_PPC: arch = "PowerPC"; break; | |
1187 | case IH_CPU_S390: arch = "IBM S390"; break; | |
1188 | case IH_CPU_SH: arch = "SuperH"; break; | |
1189 | case IH_CPU_SPARC: arch = "SPARC"; break; | |
1190 | case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break; | |
8564acf9 | 1191 | case IH_CPU_M68K: arch = "M68K"; break; |
507bbe3e | 1192 | case IH_CPU_MICROBLAZE: arch = "Microblaze"; break; |
47d1a6e1 WD |
1193 | default: arch = "Unknown Architecture"; break; |
1194 | } | |
1195 | ||
1196 | switch (hdr->ih_type) { | |
1197 | case IH_TYPE_INVALID: type = "Invalid Image"; break; | |
1198 | case IH_TYPE_STANDALONE:type = "Standalone Program"; break; | |
1199 | case IH_TYPE_KERNEL: type = "Kernel Image"; break; | |
1200 | case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break; | |
1201 | case IH_TYPE_MULTI: type = "Multi-File Image"; break; | |
1202 | case IH_TYPE_FIRMWARE: type = "Firmware"; break; | |
1203 | case IH_TYPE_SCRIPT: type = "Script"; break; | |
1204 | default: type = "Unknown Image"; break; | |
1205 | } | |
1206 | ||
1207 | switch (hdr->ih_comp) { | |
1208 | case IH_COMP_NONE: comp = "uncompressed"; break; | |
1209 | case IH_COMP_GZIP: comp = "gzip compressed"; break; | |
1210 | case IH_COMP_BZIP2: comp = "bzip2 compressed"; break; | |
1211 | default: comp = "unknown compression"; break; | |
1212 | } | |
1213 | ||
1214 | printf ("%s %s %s (%s)", arch, os, type, comp); | |
1215 | } | |
1216 | ||
1217 | #define ZALLOC_ALIGNMENT 16 | |
1218 | ||
1219 | static void *zalloc(void *x, unsigned items, unsigned size) | |
1220 | { | |
1221 | void *p; | |
1222 | ||
1223 | size *= items; | |
1224 | size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); | |
1225 | ||
1226 | p = malloc (size); | |
1227 | ||
1228 | return (p); | |
1229 | } | |
1230 | ||
1231 | static void zfree(void *x, void *addr, unsigned nb) | |
1232 | { | |
1233 | free (addr); | |
1234 | } | |
1235 | ||
1236 | #define HEAD_CRC 2 | |
1237 | #define EXTRA_FIELD 4 | |
1238 | #define ORIG_NAME 8 | |
1239 | #define COMMENT 0x10 | |
1240 | #define RESERVED 0xe0 | |
1241 | ||
1242 | #define DEFLATED 8 | |
1243 | ||
eedcd078 | 1244 | int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp) |
47d1a6e1 WD |
1245 | { |
1246 | z_stream s; | |
1247 | int r, i, flags; | |
1248 | ||
1249 | /* skip header */ | |
1250 | i = 10; | |
1251 | flags = src[3]; | |
1252 | if (src[2] != DEFLATED || (flags & RESERVED) != 0) { | |
4b9206ed | 1253 | puts ("Error: Bad gzipped data\n"); |
47d1a6e1 WD |
1254 | return (-1); |
1255 | } | |
1256 | if ((flags & EXTRA_FIELD) != 0) | |
1257 | i = 12 + src[10] + (src[11] << 8); | |
1258 | if ((flags & ORIG_NAME) != 0) | |
1259 | while (src[i++] != 0) | |
1260 | ; | |
1261 | if ((flags & COMMENT) != 0) | |
1262 | while (src[i++] != 0) | |
1263 | ; | |
1264 | if ((flags & HEAD_CRC) != 0) | |
1265 | i += 2; | |
1266 | if (i >= *lenp) { | |
4b9206ed | 1267 | puts ("Error: gunzip out of data in header\n"); |
47d1a6e1 WD |
1268 | return (-1); |
1269 | } | |
1270 | ||
1271 | s.zalloc = zalloc; | |
1272 | s.zfree = zfree; | |
1273 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) | |
1274 | s.outcb = (cb_func)WATCHDOG_RESET; | |
1275 | #else | |
1276 | s.outcb = Z_NULL; | |
1277 | #endif /* CONFIG_HW_WATCHDOG */ | |
1278 | ||
1279 | r = inflateInit2(&s, -MAX_WBITS); | |
1280 | if (r != Z_OK) { | |
1281 | printf ("Error: inflateInit2() returned %d\n", r); | |
1282 | return (-1); | |
1283 | } | |
1284 | s.next_in = src + i; | |
1285 | s.avail_in = *lenp - i; | |
1286 | s.next_out = dst; | |
1287 | s.avail_out = dstlen; | |
1288 | r = inflate(&s, Z_FINISH); | |
1289 | if (r != Z_OK && r != Z_STREAM_END) { | |
1290 | printf ("Error: inflate() returned %d\n", r); | |
1291 | return (-1); | |
1292 | } | |
1293 | *lenp = s.next_out - (unsigned char *) dst; | |
1294 | inflateEnd(&s); | |
1295 | ||
1296 | return (0); | |
1297 | } | |
1298 | ||
c29fdfc1 WD |
1299 | #ifdef CONFIG_BZIP2 |
1300 | void bz_internal_error(int errcode) | |
1301 | { | |
1302 | printf ("BZIP2 internal error %d\n", errcode); | |
1303 | } | |
1304 | #endif /* CONFIG_BZIP2 */ | |
1305 | ||
d791b1dc WD |
1306 | static void |
1307 | do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], | |
1308 | ulong addr, ulong *len_ptr, int verify) | |
1309 | { | |
1310 | DECLARE_GLOBAL_DATA_PTR; | |
1311 | image_header_t *hdr = &header; | |
1312 | void (*entry_point)(bd_t *); | |
1313 | ||
1314 | entry_point = (void (*)(bd_t *)) hdr->ih_ep; | |
1315 | ||
1316 | printf ("## Transferring control to RTEMS (at address %08lx) ...\n", | |
1317 | (ulong)entry_point); | |
1318 | ||
1319 | SHOW_BOOT_PROGRESS (15); | |
1320 | ||
1321 | /* | |
1322 | * RTEMS Parameters: | |
1323 | * r3: ptr to board info data | |
1324 | */ | |
1325 | ||
1326 | (*entry_point ) ( gd->bd ); | |
1327 | } | |
1328 | ||
47d1a6e1 WD |
1329 | #if (CONFIG_COMMANDS & CFG_CMD_ELF) |
1330 | static void | |
1331 | do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], | |
1332 | ulong addr, ulong *len_ptr, int verify) | |
1333 | { | |
1334 | image_header_t *hdr = &header; | |
1335 | char str[80]; | |
1336 | ||
1337 | sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */ | |
1338 | setenv("loadaddr", str); | |
1339 | do_bootvx(cmdtp, 0, 0, NULL); | |
1340 | } | |
1341 | ||
1342 | static void | |
1343 | do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[], | |
1344 | ulong addr, ulong *len_ptr, int verify) | |
1345 | { | |
1346 | image_header_t *hdr = &header; | |
1347 | char *local_args[2]; | |
1348 | char str[16]; | |
1349 | ||
1350 | sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */ | |
1351 | local_args[0] = argv[0]; | |
1352 | local_args[1] = str; /* and provide it via the arguments */ | |
1353 | do_bootelf(cmdtp, 0, 2, local_args); | |
1354 | } | |
1355 | #endif /* CFG_CMD_ELF */ | |
1f4bb37d WD |
1356 | |
1357 | #ifdef CONFIG_LYNXKDI | |
1358 | static void | |
1359 | do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag, | |
1360 | int argc, char *argv[], | |
1361 | ulong addr, | |
1362 | ulong *len_ptr, | |
1363 | int verify) | |
1364 | { | |
1365 | lynxkdi_boot( &header ); | |
1366 | } | |
1367 | ||
1368 | #endif /* CONFIG_LYNXKDI */ |