blob: 3759fd2d35743dc2bbfc5a46dd9d9d83c3b083b5 [file] [log] [blame]
wdenkabf7a7c2003-12-08 01:34:36 +00001/*
2 * (C) Copyright 2003
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 modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (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
wdenke65527f2004-02-12 00:47:09 +000015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkabf7a7c2003-12-08 01:34:36 +000016 * 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
wdenke65527f2004-02-12 00:47:09 +000020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
wdenkabf7a7c2003-12-08 01:34:36 +000021 *
22 */
23
24#include <common.h>
25#include <command.h>
wdenkabf7a7c2003-12-08 01:34:36 +000026#include <image.h>
27#include <zlib.h>
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050028#include <bzlib.h>
TsiChungLiew890adcb2007-10-25 17:14:00 -050029#include <watchdog.h>
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050030#include <environment.h>
wdenkabf7a7c2003-12-08 01:34:36 +000031#include <asm/byteorder.h>
32
Wolfgang Denk6405a152006-03-31 18:32:53 +020033DECLARE_GLOBAL_DATA_PTR;
34
wdenkabf7a7c2003-12-08 01:34:36 +000035#define PHYSADDR(x) x
36
wdenke65527f2004-02-12 00:47:09 +000037#define LINUX_MAX_ENVS 256
38#define LINUX_MAX_ARGS 256
wdenkabf7a7c2003-12-08 01:34:36 +000039
TsiChungLiew890adcb2007-10-25 17:14:00 -050040#define CHUNKSZ (64 * 1024)
41
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050042#ifdef CONFIG_SHOW_BOOT_PROGRESS
43# include <status_led.h>
44# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
45#else
46# define SHOW_BOOT_PROGRESS(arg)
47#endif
wdenkabf7a7c2003-12-08 01:34:36 +000048
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050049extern image_header_t header;
wdenkabf7a7c2003-12-08 01:34:36 +000050
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010051int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
52
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050053void do_bootm_linux(cmd_tbl_t * cmdtp, int flag,
54 int argc, char *argv[],
55 ulong addr, ulong * len_ptr, int verify)
wdenkabf7a7c2003-12-08 01:34:36 +000056{
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050057 ulong sp;
Marian Balakowicz41d71ed2008-01-08 18:14:09 +010058 ulong len;
wdenkabf7a7c2003-12-08 01:34:36 +000059 ulong initrd_start, initrd_end;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050060 ulong cmd_start, cmd_end;
61 ulong initrd_high;
wdenkabf7a7c2003-12-08 01:34:36 +000062 ulong data;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050063 int initrd_copy_to_ram = 1;
64 char *cmdline;
65 char *s;
66 bd_t *kbd;
67 void (*kernel) (bd_t *, ulong, ulong, ulong, ulong);
wdenkabf7a7c2003-12-08 01:34:36 +000068 image_header_t *hdr = &header;
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -050069
70 if ((s = getenv("initrd_high")) != NULL) {
71 /* a value of "no" or a similar string will act like 0,
72 * turning the "load high" feature off. This is intentional.
73 */
74 initrd_high = simple_strtoul(s, NULL, 16);
75 if (initrd_high == ~0)
76 initrd_copy_to_ram = 0;
77 } else { /* not set, no restrictions to load high */
78 initrd_high = ~0;
79 }
80
81#ifdef CONFIG_LOGBUFFER
82 kbd = gd->bd;
83 /* Prevent initrd from overwriting logbuffer */
84 if (initrd_high < (kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD))
85 initrd_high = kbd->bi_memsize - LOGBUFF_LEN - LOGBUFF_OVERHEAD;
86 debug("## Logbuffer at 0x%08lX ", kbd->bi_memsize - LOGBUFF_LEN);
87#endif
88
89 /*
90 * Booting a (Linux) kernel image
91 *
92 * Allocate space for command line and board info - the
93 * address should be as high as possible within the reach of
94 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
95 * memory, which means far enough below the current stack
96 * pointer.
97 */
98 asm("movel %%a7, %%d0\n"
99 "movel %%d0, %0\n": "=d"(sp): :"%d0");
Stefan Roesefe9dae62007-08-18 14:33:02 +0200100
101 debug("## Current stack ends at 0x%08lX ", sp);
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500102
103 sp -= 2048; /* just to be sure */
104 if (sp > CFG_BOOTMAPSZ)
105 sp = CFG_BOOTMAPSZ;
106 sp &= ~0xF;
107
108 debug("=> set upper limit to 0x%08lX\n", sp);
109
110 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
111 kbd = (bd_t *) (((ulong) cmdline - sizeof(bd_t)) & ~0xF);
112
113 if ((s = getenv("bootargs")) == NULL)
114 s = "";
wdenkabf7a7c2003-12-08 01:34:36 +0000115
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500116 strcpy(cmdline, s);
117
118 cmd_start = (ulong) & cmdline[0];
119 cmd_end = cmd_start + strlen(cmdline);
120
121 *kbd = *(gd->bd);
122
123#ifdef DEBUG
124 printf("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
125
126 do_bdinfo(NULL, 0, 0, NULL);
127#endif
128
129 if ((s = getenv("clocks_in_mhz")) != NULL) {
130 /* convert all clock information to MHz */
131 kbd->bi_intfreq /= 1000000L;
132 kbd->bi_busfreq /= 1000000L;
133 }
134
135 kernel =
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100136 (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
wdenkabf7a7c2003-12-08 01:34:36 +0000137
138 /*
139 * Check if there is an initrd image
140 */
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500141
wdenkabf7a7c2003-12-08 01:34:36 +0000142 if (argc >= 3) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500143 debug("Not skipping initrd\n");
144 SHOW_BOOT_PROGRESS(9);
wdenkabf7a7c2003-12-08 01:34:36 +0000145
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500146 addr = simple_strtoul(argv[2], NULL, 16);
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100147 hdr = (image_header_t *)addr;
wdenkabf7a7c2003-12-08 01:34:36 +0000148
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500149 printf("## Loading RAMDisk Image at %08lx ...\n", addr);
wdenkabf7a7c2003-12-08 01:34:36 +0000150
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100151 if (!image_check_magic (hdr)) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500152 puts("Bad Magic Number\n");
153 SHOW_BOOT_PROGRESS(-10);
154 do_reset(cmdtp, flag, argc, argv);
wdenkabf7a7c2003-12-08 01:34:36 +0000155 }
156
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100157 if (!image_check_hcrc (hdr)) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500158 puts("Bad Header Checksum\n");
159 SHOW_BOOT_PROGRESS(-11);
160 do_reset(cmdtp, flag, argc, argv);
wdenkabf7a7c2003-12-08 01:34:36 +0000161 }
162
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500163 SHOW_BOOT_PROGRESS(10);
wdenkabf7a7c2003-12-08 01:34:36 +0000164
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100165 print_image_hdr (hdr);
wdenkabf7a7c2003-12-08 01:34:36 +0000166
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100167 data = image_get_data (hdr);
168 len = image_get_data_size (hdr);
wdenkabf7a7c2003-12-08 01:34:36 +0000169
170 if (verify) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500171 puts(" Verifying Checksum ... ");
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100172 if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500173 puts("Bad Data CRC\n");
174 SHOW_BOOT_PROGRESS(-12);
175 do_reset(cmdtp, flag, argc, argv);
wdenkabf7a7c2003-12-08 01:34:36 +0000176 }
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500177 puts("OK\n");
wdenkabf7a7c2003-12-08 01:34:36 +0000178 }
179
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500180 SHOW_BOOT_PROGRESS(11);
wdenkabf7a7c2003-12-08 01:34:36 +0000181
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100182 if (!image_check_os (hdr, IH_OS_LINUX) ||
183 !image_check_arch (hdr, IH_ARCH_M68K) ||
184 !image_check_type (hdr, IH_TYPE_RAMDISK)) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500185 puts("No Linux ColdFire Ramdisk Image\n");
186 SHOW_BOOT_PROGRESS(-13);
187 do_reset(cmdtp, flag, argc, argv);
wdenkabf7a7c2003-12-08 01:34:36 +0000188 }
189
190 /*
191 * Now check if we have a multifile image
192 */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100193 } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
194 u_long tail = image_to_cpu (len_ptr[0]) % 4;
wdenkabf7a7c2003-12-08 01:34:36 +0000195 int i;
196
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500197 SHOW_BOOT_PROGRESS(13);
wdenkabf7a7c2003-12-08 01:34:36 +0000198
199 /* skip kernel length and terminator */
200 data = (ulong) (&len_ptr[2]);
201 /* skip any additional image length fields */
202 for (i = 1; len_ptr[i]; ++i)
203 data += 4;
204 /* add kernel length, and align */
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100205 data += image_to_cpu (len_ptr[0]);
wdenkabf7a7c2003-12-08 01:34:36 +0000206 if (tail) {
207 data += 4 - tail;
208 }
209
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100210 len = image_to_cpu (len_ptr[1]);
wdenkabf7a7c2003-12-08 01:34:36 +0000211
212 } else {
213 /*
214 * no initrd image
215 */
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500216 SHOW_BOOT_PROGRESS(14);
wdenkabf7a7c2003-12-08 01:34:36 +0000217
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500218 len = data = 0;
wdenkabf7a7c2003-12-08 01:34:36 +0000219 }
220
wdenkabf7a7c2003-12-08 01:34:36 +0000221 if (!data) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500222 debug("No initrd\n");
wdenkabf7a7c2003-12-08 01:34:36 +0000223 }
wdenkabf7a7c2003-12-08 01:34:36 +0000224
225 if (data) {
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500226 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
227 initrd_start = data;
228 initrd_end = initrd_start + len;
229 } else {
230 initrd_start = (ulong) kbd - len;
231 initrd_start &= ~(4096 - 1); /* align on page */
wdenkabf7a7c2003-12-08 01:34:36 +0000232
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500233 if (initrd_high) {
234 ulong nsp;
wdenkabf7a7c2003-12-08 01:34:36 +0000235
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500236 /*
237 * the inital ramdisk does not need to be within
238 * CFG_BOOTMAPSZ as it is not accessed until after
239 * the mm system is initialised.
240 *
241 * do the stack bottom calculation again and see if
242 * the initrd will fit just below the monitor stack
243 * bottom without overwriting the area allocated
244 * above for command line args and board info.
245 */
246 asm("movel %%a7, %%d0\n"
247 "movel %%d0, %0\n": "=d"(nsp): :"%d0");
Stefan Roesefe9dae62007-08-18 14:33:02 +0200248
249 nsp -= 2048; /* just to be sure */
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500250 nsp &= ~0xF;
wdenkabf7a7c2003-12-08 01:34:36 +0000251
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500252 if (nsp > initrd_high) /* limit as specified */
253 nsp = initrd_high;
wdenkabf7a7c2003-12-08 01:34:36 +0000254
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500255 nsp -= len;
256 nsp &= ~(4096 - 1); /* align on page */
wdenkabf7a7c2003-12-08 01:34:36 +0000257
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500258 if (nsp >= sp)
259 initrd_start = nsp;
260 }
wdenkabf7a7c2003-12-08 01:34:36 +0000261
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500262 SHOW_BOOT_PROGRESS(12);
wdenkabf7a7c2003-12-08 01:34:36 +0000263
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500264 debug
265 ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
266 data, data + len - 1, len, len);
wdenkabf7a7c2003-12-08 01:34:36 +0000267
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500268 initrd_end = initrd_start + len;
269 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
270 initrd_start, initrd_end);
271#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
272 {
273 size_t l = len;
274 void *to = (void *)initrd_start;
275 void *from = (void *)data;
wdenkabf7a7c2003-12-08 01:34:36 +0000276
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500277 while (l > 0) {
278 size_t tail =
279 (l > CHUNKSZ) ? CHUNKSZ : l;
280 WATCHDOG_RESET();
281 memmove(to, from, tail);
282 to += tail;
283 from += tail;
284 l -= tail;
285 }
wdenkabf7a7c2003-12-08 01:34:36 +0000286 }
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500287#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
288 memmove((void *)initrd_start, (void *)data, len);
289#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
290 puts("OK\n");
wdenkabf7a7c2003-12-08 01:34:36 +0000291 }
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500292 } else {
293 initrd_start = 0;
294 initrd_end = 0;
wdenkabf7a7c2003-12-08 01:34:36 +0000295 }
296
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500297 debug("## Transferring control to Linux (at address %08lx) ...\n",
298 (ulong) kernel);
wdenkabf7a7c2003-12-08 01:34:36 +0000299
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500300 SHOW_BOOT_PROGRESS(15);
wdenkabf7a7c2003-12-08 01:34:36 +0000301
TsiChungLiewfc3ca3b2007-08-16 15:05:11 -0500302 /*
303 * Linux Kernel Parameters (passing board info data):
304 * r3: ptr to board info data
305 * r4: initrd_start or 0 if no initrd
306 * r5: initrd_end - unused if r4 is 0
307 * r6: Start of command line string
308 * r7: End of command line string
309 */
310 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
311 /* does not return */
wdenkabf7a7c2003-12-08 01:34:36 +0000312}